Vue从入门到实战:render函数
来自CloudWiki
Vue推荐在大多数情况下使用模板来构建HTML,
然后在一些场景中,你可能需要Javascript的编程能力,这时可以使用render函数,它比模板更接近编译器。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="vue.js"></script> </head> <body> <div id="app"> <anchored-heading :level="1"> <a name="hello-world" href="#hello-world"> Hello world! </a> </anchored-heading> </div> <script type = "text/javascript"> Vue.component('anchored-heading', { render: function (createElement) { return createElement( 'h' + this.level, // 标签名称 this.$slots.default // 子节点数组 ) }, props: { level: { type: Number, required: true } } }) new Vue({ el: '#app' }) </script> </body> </html>