1.什么是render函数
- 可能是通过template解析出来的,也有可能是自己写的
export function renderMixin(Vue){
Vue.prototype._c = function(){
return createElement(this,...arguments)
}
Vue.prototype._v = function (text) {
return createTextElement(this,text)
}
Vue.prototype._s = function(val){
if(typeof val == 'object') return JSON.stringify(val)
return val;
}
Vue.prototype._render = function(){
const vm = this;
let render = vm.$options.render;
let vnode = render.call(vm);
return vnode;
}
}
2.vnode虚拟dom
function vnode(vm, tag, data, key, children, text) {
return {
vm,
tag,
data,
key,
children,
text,
// .....
}
}
3.createElement函数,创建虚拟节点
export function createElement(vm, tag, data = {}, ...children) {
return vnode(vm, tag, data, data.key, children, undefined);
}
4.createTextElement函数,创建虚拟文本
export function createTextElement(vm, text) {
return vnode(vm, undefined, undefined, undefined, undefined, text);
}
5.生成最后的vode
{
"vm": {
"$options": {},
"_data": {
"arr": {
"name": "zf"
}
},
"$el": {}
},
"tag": "div",
"data": {
"id": "app",
"a": "1",
"style": {
"color": "red",
"background": "blue"
}
},
"children": [
{
"vm": {
"$options": {},
"_data": {
"arr": {
"name": "zf"
}
},
"$el": {}
},
"text": "hello{"name":"zf"}world"
}
]
}