本文共 2080 字,大约阅读时间需要 6 分钟。
Virtual Dom并不是真正意义上的DOM,而是一个轻量级的JavaScript对象,在状态发生变化时,Virtual Dom会进行Diff运算,来更新只需要被替换的DOM,而不是全部重绘。
与DOM操作相比,Virtual Dom是基于JavaScript计算的,所以开销会小很多。
Virtual Dom运行过程:
文本内容
文本内容
用Virtual Dom创建的JavaScript对象一般会是这样的:
var vNode = { tag: 'div', attributes:{ id: 'main' }, children: { //p节点 }}
在Vue.js 2 中,Virtual Dom 就是通过一种VNode类表达的,每个DOM元素或组件都对应一个VNode对象,在Vue.js 源码中是这样定义的:
export interface VNode{ tag?: string; data?: VNodeData; children?: VNode[]; text?: string; elm?: Node; ns?: string; context?: Vue; key?: string | number; componentOptions?: VNodeComponentOptions; componentInstance?: Vue; parent?: VNode; raw?: boolean; isStatic?: boolean; isRootInsert: boolean; isComment: boolean;}
VNodeData代码如下:
export interface VNodeData{ key?: string | number; slot?: string; scopedSlots?: { [key: string]: ScopedSlot}; ref?: string; tag?: string; staticClass?: string; class?: any; staticStyle?: { [key: string]: any}; style?: { [key: string]: any}; props?: { [key: string]: any}; attrs?: { [key: string]: any}; domProps?: { [key: string]: any}; hook?: { [key: string]: Function}; on?: { [key: string]: Function | Function[]}; transition?: Object; show?: boolean; inlineTemplate?: { render: Function; staticRenderFns: Function[]; }; directives?: VNodeDirective[]; keepAlive?: boolean;}
具体含义如下:
转载地址:http://masqb.baihongyu.com/