vue程序运行过程:
template —解析–> ast –编译–>render函数 --^virtual tree^–> UI
在vue init webpack myproject
之后,有Vue build 选项: runtime compiler 和 runtime-only
两种选择生成的项目区别,只在main.js中
runtime compiler - main.js:
import Vue from 'vue'
import App from './App'
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
components: { App },
template: '<App/>'
})
template > ast > render > vdom > ui
runtime-only - main.js:
import Vue from 'vue'
import App from './App'
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
render: h => h(App)
})
.vue文件中的template是由 vue-template-compiler 处理
render > vdom > ui
runtime-only 的性能更高,代码量更少
render函数
...
render: h => h(App)
// 实际上
render: function(createElement){
// 1. createElement('标签',{标签的属性},[])
// 是一个函数,接受三个参数:
// 第一个参数,是标签名,会生成对应标签,替代vue实例挂载的dom
// 第二个参数,对象,存放标签的属性 eg. {class: box},可不写
// 第三个参数,数组,存放内容 ['hello world']
// 1. 普通用法
return createElement('h2',{id: box},['hello, render']) --常规使用
return createElement('h2',
{id: box},
['hello, render',createElement('button',['按钮']])
// 2. 传入一个组件
return createElement(cpn)
}