render function会被转化成VNode节点
一个结构类似于真实DOM的js对象
class VNode {
constructor (tag, data, children, text, elm) {
// 当前节点的标签名
this.tag = tag;
// 当前节点的一些数据信息
this.data = data;
// 当前节点的文本
this.text = text;
// 当前虚拟节点对应的真实DOM
this.elm = elm;
}
}
<template>
<span class="demo" v-show="isShow">
This is a span.
</span>
</template>
function render () {
return new VNode(
‘span‘,
{
// 指令集合数组
directives: [
{
// v-show指令
rawName: ‘v-show‘,
expression: ‘isShow‘,
name: ‘show‘,
value: true
}
],
// 静态class
staticClass: ‘demo‘
},
[new VNode(undefined, undefined, undefined, ‘This is a span.‘)]
);
}
{
tag: ‘span‘,
data: {
// 指令集合数组
directives: [
{
rawName: ‘v-show‘,
expression: ‘isShow‘,
name: ‘show‘,
value: true
}
],
// 静态class
staticClass: ‘demo‘
},
text: undefined,
children: [
// 子节点是一个文本VNode节点
{
tag: undefined,
data: undefined,
text: ‘This is a span.‘,
children: undefined
}
]
}
function createEmpty VNode () {
const node = new VNode();
node.text = ‘‘;
return node;
}
function createTextVNode (val) {
return new VNode(undefined, undefined, undefined, String(val));
}
function cloneVNode (node) {
const cloneVnoed = new VNode(
node.tag,
node.data,
node.children,
node.text,
node.elm
);
return cloneVnode;
}
原文:https://www.cnblogs.com/pleaseAnswer/p/14331209.html