国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

vue的diff算法知識點總結

2024-05-06 15:20:18
字體:
來源:轉載
供稿:網友

源碼:https://github.com/vuejs/vue/blob/dev/src/core/vdom/patch.js

虛擬dom

diff算法首先要明確一個概念就是diff的對象是虛擬dom,更新真實dom則是diff算法的結果

Vnode基類

 constructor (  。。。 ) {  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 }

這個部分的代碼 主要是為了更好地知道在diff算法中具體diff的屬性的含義,當然也可以更好地了解vnode實例

整體過程

核心函數是patch函數

isUndef判斷(是不是undefined或者null) // empty mount (likely as component), create new root elementcreateElm(vnode, insertedVnodeQueue) 這里可以發現創建節點不是一個一個插入,而是放入一個隊列中統一批處理
核心函數sameVnode
function sameVnode (a, b) { return (  a.key === b.key && (   (    a.tag === b.tag &&    a.isComment === b.isComment &&    isDef(a.data) === isDef(b.data) &&    sameInputType(a, b)   ) || (    isTrue(a.isAsyncPlaceholder) &&    a.asyncFactory === b.asyncFactory &&    isUndef(b.asyncFactory.error)   )  ) )}

這里是一個外層的比較函數,直接去比較了兩個節點的key,tag(標簽),data的比較(注意這里的data指的是VNodeData),input的話直接比較type。

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?: object[] | object; props?: { [key: string]: any }; attrs?: { [key: string]: any }; domProps?: { [key: string]: any }; hook?: { [key: string]: Function }; on?: { [key: string]: Function | Function[] }; nativeOn?: { [key: string]: Function | Function[] }; transition?: object; show?: boolean; inlineTemplate?: {  render: Function;  staticRenderFns: Function[]; }; directives?: VNodeDirective[]; keepAlive?: boolean;}

這會確認兩個節點是否有進一步比較的價值,不然直接替換

替換的過程主要是一個createElm函數 另外則是銷毀oldVNode

// destroy old node    if (isDef(parentElm)) {     removeVnodes(parentElm, [oldVnode], 0, 0)    } else if (isDef(oldVnode.tag)) {     invokeDestroyHook(oldVnode)    }            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 长汀县| 民勤县| 行唐县| 怀柔区| 精河县| 鄂温| 儋州市| 大余县| 灵台县| 商都县| 梅州市| 泾阳县| 方城县| 博兴县| 漳州市| 东台市| 鄂托克旗| 湛江市| 英山县| 乡宁县| 吉隆县| 马鞍山市| 化德县| 博野县| 潢川县| 道孚县| 延庆县| 报价| 兰溪市| 永顺县| 杭州市| 许昌县| 崇文区| 涟源市| 佛教| 红河县| 永春县| 南雄市| 年辖:市辖区| 玉龙| 博客|