render: h => h(App)
是下面内容的缩写:
render: function (createElement) {
return createElement(App);
}
进一步缩写为(ES6 语法):
render (createElement) {
return createElement(App);
}
再进一步缩写为:
render (h){
return h(App);
}
按照 ES6 箭头函数的写法,就得到了:
render: h => h(App);
其中 根据 Vue.js 作者 Even You 的回复,h 的含义如下:
It comes from the term “hyperscript”, which is commonly used in many virtual-dom implementations. “Hyperscript” itself stands for “script that generates HTML structures” because HTML is the acronym for “hyper-text markup language”.
它来自单词 hyperscript
,这个单词通常用在 virtual-dom
的实现中。Hyperscript
本身是指生成HTML
结构的 script
脚本,因为 HTML 是 hyper-text markup language
的缩写(超文本标记语言)
个人理解:createElement
函数是用来生成 HTML DOM
元素的,也就是上文中的 generate HTML structures
,也就是 Hyperscript
,这样作者才把 createElement
简写成 h
。
// 写法1:
import App from './App'
new Vue({
el: '#root',
render: h => h(App)
})
// 写法2:
import App from './App'
new Vue({
el: '#root',
template: '<App></App>',
components: {
App
}
})
上面两种的效果是一样的,可以看出 h(App)
函数 的作用是:使用App作为这个Vue实例的template(同时一并了注册App组件)