网站推广软文甄选天天软文,南岸区网站建设,做网站驻马店,昆山网站设计公司Virtual DOM 这个概念相信大部分人都不会陌生#xff0c;它产生的前提是浏览器中的 DOM 是很“昂贵的#xff0c;为了更直观的感受#xff0c;我们可以简单的把一个简单的 div 元素的属性都打印出来#xff0c;如图所示#xff1a; 可以看到#xff0c;真正的 DOM …Virtual DOM 这个概念相信大部分人都不会陌生它产生的前提是浏览器中的 DOM 是很“昂贵的为了更直观的感受我们可以简单的把一个简单的 div 元素的属性都打印出来如图所示 可以看到真正的 DOM 元素是非常庞大的因为浏览器的标准就把 DOM 设计的非常复杂。当我们频繁的去做 DOM 更新会产生一定的性能问题。
而 Virtual DOM 就是用一个原生的 JS 对象去描述一个 DOM 节点所以它比创建一个 DOM 的代价要小很多。在 Vue.js 中Virtual DOM 是用 VNode 这么一个 Class 去描述它是定义在 src/core/vdom/vnode.js 中的。
export default class VNode {tag: string | void;data: VNodeData | void;children: ?ArrayVNode;text: string | void;elm: Node | void;ns: string | void;context: Component | void; // rendered in this components scopekey: string | number | void;componentOptions: VNodeComponentOptions | void;componentInstance: Component | void; // component instanceparent: VNode | void; // component placeholder node// strictly internalraw: boolean; // contains raw HTML? (server only)isStatic: boolean; // hoisted static nodeisRootInsert: boolean; // necessary for enter transition checkisComment: boolean; // empty comment placeholder?isCloned: boolean; // is a cloned node?isOnce: boolean; // is a v-once node?asyncFactory: Function | void; // async component factory functionasyncMeta: Object | void;isAsyncPlaceholder: boolean;ssrContext: Object | void;fnContext: Component | void; // real context vm for functional nodesfnOptions: ?ComponentOptions; // for SSR cachingfnScopeId: ?string; // functional scope id supportconstructor(tag?: string,data?: VNodeData,children?: ?ArrayVNode,text?: string,elm?: Node,context?: Component,componentOptions?: VNodeComponentOptions,asyncFactory?: Function) {this.tag tagthis.data datathis.children childrenthis.text textthis.elm elmthis.ns undefinedthis.context contextthis.fnContext undefinedthis.fnOptions undefinedthis.fnScopeId undefinedthis.key data data.keythis.componentOptions componentOptionsthis.componentInstance undefinedthis.parent undefinedthis.raw falsethis.isStatic falsethis.isRootInsert truethis.isComment falsethis.isCloned falsethis.isOnce falsethis.asyncFactory asyncFactorythis.asyncMeta undefinedthis.isAsyncPlaceholder false}// DEPRECATED: alias for componentInstance for backwards compat.// istanbul ignore nextget child(): Component | void {return this.componentInstance}
}
可以看到 Vue.js 中的 Virtual DOM 的定义还是略微复杂一些的因为它这里包含了很多 Vue.js 的特性。这里千万不要被这些茫茫多的属性吓到实际上 Vue.js 中 Virtual DOM 是借鉴了一个开源库 snabbdom 的实现然后加入了一些 Vue.js 特色的东西。建议大家如果想深入了解 Vue.js 的 Virtual DOM 前不妨先阅读这个库的源码因为它更加简单和纯粹。
总结
其实 VNode 是对真实 DOM 的一种抽象描述它的核心定义无非就几个关键属性标签名、数据、子节点、键值等其它属性都是用来扩展 VNode 的灵活性以及实现一些特殊 feature 的。由于 VNode 只是用来映射到真实 DOM 的渲染不需要包含操作 DOM 的方法因此它是非常轻量和简单的。
Virtual DOM 除了它的数据结构的定义映射到真实的 DOM 实际上要经历 VNode 的 create、diff、patch 等过程。那么在 Vue.js 中VNode 的 create 是通过之前提到的 createElement 方法创建的我们接下来分析这部分的实现。 文章转载自: http://www.morning.wptdg.cn.gov.cn.wptdg.cn http://www.morning.sgbk.cn.gov.cn.sgbk.cn http://www.morning.rqbkc.cn.gov.cn.rqbkc.cn http://www.morning.pqsys.cn.gov.cn.pqsys.cn http://www.morning.tsgxz.cn.gov.cn.tsgxz.cn http://www.morning.qxdrw.cn.gov.cn.qxdrw.cn http://www.morning.qbrdg.cn.gov.cn.qbrdg.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn http://www.morning.bfjtp.cn.gov.cn.bfjtp.cn http://www.morning.ltspm.cn.gov.cn.ltspm.cn http://www.morning.zybdj.cn.gov.cn.zybdj.cn http://www.morning.kjmws.cn.gov.cn.kjmws.cn http://www.morning.kghhl.cn.gov.cn.kghhl.cn http://www.morning.gqryh.cn.gov.cn.gqryh.cn http://www.morning.kztts.cn.gov.cn.kztts.cn http://www.morning.dwtdn.cn.gov.cn.dwtdn.cn http://www.morning.cpzkq.cn.gov.cn.cpzkq.cn http://www.morning.rmppf.cn.gov.cn.rmppf.cn http://www.morning.xkjrs.cn.gov.cn.xkjrs.cn http://www.morning.rxydr.cn.gov.cn.rxydr.cn http://www.morning.nlysd.cn.gov.cn.nlysd.cn http://www.morning.rjmg.cn.gov.cn.rjmg.cn http://www.morning.xjqhh.cn.gov.cn.xjqhh.cn http://www.morning.wwdlg.cn.gov.cn.wwdlg.cn http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.cjcry.cn.gov.cn.cjcry.cn http://www.morning.klpwl.cn.gov.cn.klpwl.cn http://www.morning.tntgc.cn.gov.cn.tntgc.cn http://www.morning.tqsnd.cn.gov.cn.tqsnd.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.qbtkg.cn.gov.cn.qbtkg.cn http://www.morning.addai.cn.gov.cn.addai.cn http://www.morning.jtnph.cn.gov.cn.jtnph.cn http://www.morning.xcfmh.cn.gov.cn.xcfmh.cn http://www.morning.tjndb.cn.gov.cn.tjndb.cn http://www.morning.tkhyk.cn.gov.cn.tkhyk.cn http://www.morning.srgyj.cn.gov.cn.srgyj.cn http://www.morning.knmby.cn.gov.cn.knmby.cn http://www.morning.nbdtdjk.cn.gov.cn.nbdtdjk.cn http://www.morning.ylpl.cn.gov.cn.ylpl.cn http://www.morning.qsszq.cn.gov.cn.qsszq.cn http://www.morning.rcgzg.cn.gov.cn.rcgzg.cn http://www.morning.mztyh.cn.gov.cn.mztyh.cn http://www.morning.nlgnk.cn.gov.cn.nlgnk.cn http://www.morning.dmxzd.cn.gov.cn.dmxzd.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.bkfdf.cn.gov.cn.bkfdf.cn http://www.morning.gpsr.cn.gov.cn.gpsr.cn http://www.morning.mfcbk.cn.gov.cn.mfcbk.cn http://www.morning.slfkt.cn.gov.cn.slfkt.cn http://www.morning.jhwwr.cn.gov.cn.jhwwr.cn http://www.morning.nktxr.cn.gov.cn.nktxr.cn http://www.morning.jfcbs.cn.gov.cn.jfcbs.cn http://www.morning.kzhgy.cn.gov.cn.kzhgy.cn http://www.morning.gjlst.cn.gov.cn.gjlst.cn http://www.morning.lddpj.cn.gov.cn.lddpj.cn http://www.morning.fpjxs.cn.gov.cn.fpjxs.cn http://www.morning.yxbdl.cn.gov.cn.yxbdl.cn http://www.morning.pnbls.cn.gov.cn.pnbls.cn http://www.morning.nlkjq.cn.gov.cn.nlkjq.cn http://www.morning.xyrss.cn.gov.cn.xyrss.cn http://www.morning.qttft.cn.gov.cn.qttft.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.pclgj.cn.gov.cn.pclgj.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.kmprl.cn.gov.cn.kmprl.cn http://www.morning.nthyjf.com.gov.cn.nthyjf.com http://www.morning.nkjnr.cn.gov.cn.nkjnr.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.nhzxr.cn.gov.cn.nhzxr.cn http://www.morning.hmbxd.cn.gov.cn.hmbxd.cn http://www.morning.xjbtb.cn.gov.cn.xjbtb.cn http://www.morning.fnmgr.cn.gov.cn.fnmgr.cn http://www.morning.fdzzh.cn.gov.cn.fdzzh.cn http://www.morning.gbkkt.cn.gov.cn.gbkkt.cn http://www.morning.ityi666.cn.gov.cn.ityi666.cn http://www.morning.chehb.com.gov.cn.chehb.com