说说 Vue.js 中的 Virtual DOM 技术

Virtual DOM 不是 DOM ,它是一个轻量级的 JavaScript 对象,当状态发生变化时, Virtual DOM 会进行 Diff 运算,只更新那些需要真正被替换的 DOM 。

因为 Virtual DOM 只是基于 JavaScript 进行计算,所以相对于 DOM 来说,性能更高,开销更小。

Vue.js 2 中的 Virtual DOM 是通过 VNode 来实现的,每个 DOM 元素或组件都会对应一个 VNode 对象:

var VNode = function VNode (
        tag,
        data,
        children,
        text,
        elm,
        context,
        componentOptions
    ) {
        this.tag = tag;
        this.data = data;
        this.children = children;
        this.text = text;
        this.elm = elm;
        this.ns = undefined;
        this.context = context;
        this.functionalContext = undefined;
        this.key = data && data.key;
        this.componentOptions = componentOptions;
        this.componentInstance = undefined;
        this.parent = undefined;
        this.raw = false;
        this.isStatic = false;
        this.isRootInsert = true;
        this.isComment = false;
        this.isCloned = false;
        this.isOnce = false;
    };
复制代码
参数类型说明
tagstring当前节点标签名。
dataVNodeData当前节点数据对象 。
childrenVnode[]当前节点的子节点数组。
textstring当前节点文本。
elmNode当前节点所对应的 DOM 节点。
nsstring当前节点命名空间。
contextVue编译作用域。
functionalContext-函数化组件作用域。
keystring 或 number节点的 key。
componentOptionsVnodeCopmonentOptions组件的 option 选项。
componentInstanceVue当前节点所对应的组件实例。
parentVNode当前节点的父节点。
rawboolean是否为原生 HTML,innerHTML 为 true,textContent 时为 false。
isStaticboolean是否为静态节点。
isRootInsertboolean是否做为根节点被插入。
isCommentboolean是否为注释节点。
isClonedboolean是否为克隆节点。
isOnceboolean是否有 v-once 指令。

VNode 分为以下类型:

类型说明
TextVNode文本节点。
ElementVNode元素节点。
ComponentVNode组件节点。
EmptyVNode注释节点。
CloneVNode以上任意类型的拷贝节点,区别就是它的 isCloned 为 true。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值