微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Vue中简单的虚拟DOM是什么样

1. 一个简单的虚拟DOM长什么样

其实当今前端框架不少用到了虚拟DOM的技术,但各家有各家的实现。这里我们先看下比较简单的虚拟DOM库snabbdom的虚拟DOM长什么样

我们假设有html如下,其实也就是所谓的真实DOM

<div class="message">Hello World</div>

那么snabbdom对应的虚拟DOM是长以下这个样子的:

{
    "sel": "div","data": {
        "class": "message"
    },"text": "Hello World"
}

这样看来一个虚拟DOM其实就是一个json文件,里面的内容也不难理解,猜都能猜出来:

从这里就不难理解虚拟DOM和真实DOM的关系,虚拟DOM就是一个用来描述真实DOM节点的Javascript对象。

虚拟DOM除了可以描述单一的一个真实DOM节点,还能描述一颗DOM数。

比如有html如下:

<div id="container">
  <div class="message">Hello World</div>
</div>

对应的虚拟DOM将会是

{
    "sel": "div","data": {
        "id": "container"
    },"children": [
        {
            "sel": "div","data": {
                "class": "message"
            },"text": "Hello World"
        }
    ]
}

虚拟DOM对象里面多了个children的数组选项,里面的内容就是前面html中的子节点。

2. Vue中的虚拟DOM长什么样

还是以最简单的html节点为例子

<div class="message">Hello World</div>

vue中的虚拟DOM将会是如下这个样子的

{
  tag: "div",data: {
      "staticclass": "message"
  }
  children: [
    {
      text: "Hello World",...
    },],...
}

Vue中的虚拟DOM比snabbdom的虚拟DOM复杂,会多出很多属性,我这里就不一一列出来了,上面只是显示了一些我觉得和snabbdom的虚拟DOM差不多的属性

两个虚拟DOM看上去差不多,只是:

  • 标签名这里用tag而非sel
  • 标签间的文本在vue中用一个文本虚拟子节点来表示,而不是像snabbdom那样直接放到自身的text中

3. Vue中的虚拟DOM实现

在Vue中,虚拟DOM是通过VNode这个类来实现的。

export default class VNode {
  tag: string | void;
  data: VNodeData | void;
  children: ?Array<VNode>;
  text: string | void;
  elm: Node | void;
  ns: string | void;
  context: Component | void; // rendered in this component's scope
  key: string | number | void;
  componentOptions: VNodeComponentOptions | void;
  componentInstance: Component | void; // component instance
  parent: VNode | void; // component placeholder node
  // strictly internal
  raw: boolean; // contains raw HTML? (server only)
  isstatic: boolean; // hoisted static node
  isRootInsert: boolean; // necessary for enter transition check
  isComment: boolean; // empty comment placeholder?
  isCloned: boolean; // is a cloned node?
  isOnce: boolean; // is a v-once node?
  asyncFactory: Function | void; // async component factory function
  asyncMeta: Object | void;
  isAsyncPlaceholder: boolean;
  ssrContext: Object | void;
  fnContext: Component | void; // real context vm for functional nodes
  fnoptions: ?ComponentOptions; // for SSR caching
  devtoolsMeta: ?Object; // used to store functional render context for devtools
  fnScopeId: ?string; // functional scope id support
  constructor(
    tag?: string,data?: VNodeData,children?: ?Array<VNode>,text?: string,elm?: Node,context?: Component,componentOptions?: VNodeComponentOptions,asyncFactory?: Function
  ) {
    this.tag = tag;
    this.data = data;
    this.children = children;
    this.text = text;
    this.elm = elm;
    this.ns = undefined;
    this.context = context;
    this.fnContext = undefined;
    this.fnoptions = undefined;
    this.fnScopeId = 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;
    this.asyncFactory = asyncFactory;
    this.asyncMeta = undefined;
    this.isAsyncPlaceholder = false;
  }
}

其中包含了我们上面示例看到的最重要的tag,data,children等描述一个DOM结构的选项,还有很多Vue实现需要用到的选项,这里就不一一解析,什么时候用到什么时候分析吧,先有个概念就好了。

4. createTextVNode

从VNode的构造函数可以看到其接受的参数比较多,为了方便使用,vue为创建VNode节点提供了一些函数的封装,其中我们最常用的就是创建一个文本节点。

export function createTextVNode(val: string | number) {
  return new VNode(undefined,undefined,String(val));
}

对应的真实DOM

<div>Hello world</div>

原文地址:https://techgogogo.blog.csdn.net/article/details/127454556

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐