Vue从入门到实战:render函数

来自CloudWiki
Cloud17讨论 | 贡献2021年2月20日 (六) 02:05的版本 (创建页面,内容为“Vue推荐在大多数情况下使用模板来构建HTML, 然后在一些场景中,你可能需要Javascript的编程能力,这时可以使用render函数,它…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

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>