您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“vue中diff算法知識(shí)點(diǎn)有哪些”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“vue中diff算法知識(shí)點(diǎn)有哪些”這篇文章吧。
虛擬dom
diff算法首先要明確一個(gè)概念就是diff的對(duì)象是虛擬dom,更新真實(shí)dom則是diff算法的結(jié)果
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 }
這個(gè)部分的代碼 主要是為了更好地知道在diff算法中具體diff的屬性的含義,當(dāng)然也可以更好地了解vnode實(shí)例
整體過程
核心函數(shù)是patch函數(shù)
isUndef判斷(是不是undefined或者null)
// empty mount (likely as component), create new root elementcreateElm(vnode, insertedVnodeQueue) 這里可以發(fā)現(xiàn)創(chuàng)建節(jié)點(diǎn)不是一個(gè)一個(gè)插入,而是放入一個(gè)隊(duì)列中統(tǒng)一批處理
核心函數(shù)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) ) ) ) }
這里是一個(gè)外層的比較函數(shù),直接去比較了兩個(gè)節(jié)點(diǎn)的key,tag(標(biāo)簽),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; }
這會(huì)確認(rèn)兩個(gè)節(jié)點(diǎn)是否有進(jìn)一步比較的價(jià)值,不然直接替換
替換的過程主要是一個(gè)createElm函數(shù) 另外則是銷毀oldVNode
// destroy old node if (isDef(parentElm)) { removeVnodes(parentElm, [oldVnode], 0, 0) } else if (isDef(oldVnode.tag)) { invokeDestroyHook(oldVnode) }
插入過程簡(jiǎn)化來(lái)說(shuō)就是判斷node的type分別調(diào)用
createComponent(會(huì)判斷是否有children然后遞歸調(diào)用)
createComment
createTextNode
創(chuàng)建后使用insert函數(shù)
之后需要用hydrate函數(shù)將虛擬dom和真是dom進(jìn)行映射
function insert (parent, elm, ref) { if (isDef(parent)) { if (isDef(ref)) { if (ref.parentNode === parent) { nodeOps.insertBefore(parent, elm, ref) } } else { nodeOps.appendChild(parent, elm) } } }
核心函數(shù)
function patchVnode (oldVnode, vnode, insertedVnodeQueue, removeOnly) { if (oldVnode === vnode) { return } const elm = vnode.elm = oldVnode.elm if (isTrue(oldVnode.isAsyncPlaceholder)) { if (isDef(vnode.asyncFactory.resolved)) { hydrate(oldVnode.elm, vnode, insertedVnodeQueue) } else { vnode.isAsyncPlaceholder = true } return } if (isTrue(vnode.isStatic) && isTrue(oldVnode.isStatic) && vnode.key === oldVnode.key && (isTrue(vnode.isCloned) || isTrue(vnode.isOnce)) ) { vnode.componentInstance = oldVnode.componentInstance return } let i const data = vnode.data if (isDef(data) && isDef(i = data.hook) && isDef(i = i.prepatch)) { i(oldVnode, vnode) } const oldCh = oldVnode.children const ch = vnode.children if (isDef(data) && isPatchable(vnode)) { for (i = 0; i < cbs.update.length; ++i) cbs.update[i](oldVnode, vnode) if (isDef(i = data.hook) && isDef(i = i.update)) i(oldVnode, vnode) } if (isUndef(vnode.text)) { if (isDef(oldCh) && isDef(ch)) { if (oldCh !== ch) updateChildren(elm, oldCh, ch, insertedVnodeQueue, removeOnly) } else if (isDef(ch)) { if (isDef(oldVnode.text)) nodeOps.setTextContent(elm, '') addVnodes(elm, null, ch, 0, ch.length - 1, insertedVnodeQueue) } else if (isDef(oldCh)) { removeVnodes(elm, oldCh, 0, oldCh.length - 1) } else if (isDef(oldVnode.text)) { nodeOps.setTextContent(elm, '') } } else if (oldVnode.text !== vnode.text) { nodeOps.setTextContent(elm, vnode.text) } if (isDef(data)) { if (isDef(i = data.hook) && isDef(i = i.postpatch)) i(oldVnode, vnode) } }
const el = vnode.el = oldVnode.el 這是很重要的一步,讓vnode.el引用到現(xiàn)在的真實(shí)dom,當(dāng)el修改時(shí),vnode.el會(huì)同步變化。
比較二者引用是否一致
之后asyncFactory不知道是做什么的,所以這個(gè)比較看不懂
靜態(tài)節(jié)點(diǎn)比較key,相同后也不做重新渲染,直接拷貝componentInstance(once命令在此生效)
如果vnode是文本節(jié)點(diǎn)或注釋節(jié)點(diǎn),但是vnode.text != oldVnode.text時(shí),只需要更新vnode.elm的文本內(nèi)容就可以
children的比較
如果只有oldVnode有子節(jié)點(diǎn),那就把這些節(jié)點(diǎn)都刪除
如果只有vnode有子節(jié)點(diǎn),那就創(chuàng)建這些子節(jié)點(diǎn),這里如果oldVnode是個(gè)文本節(jié)點(diǎn)就把vnode.elm的文本設(shè)置為空字符串
都有則updateChildren,這個(gè)之后詳述
如果oldVnode和vnode都沒有子節(jié)點(diǎn),但是oldVnode是文本節(jié)點(diǎn)或注釋節(jié)點(diǎn),就把vnode.elm的文本設(shè)置為空字符串
updateChildren
這部分重點(diǎn)還是關(guān)注整個(gè)算法
首先四個(gè)指針,oldStart,oldEnd,newStart,newEnd,兩個(gè)數(shù)組,oldVnode,Vnode。
function updateChildren (parentElm, oldCh, newCh, insertedVnodeQueue, removeOnly) { let oldStartIdx = 0 let newStartIdx = 0 let oldEndIdx = oldCh.length - 1 let oldStartVnode = oldCh[0] let oldEndVnode = oldCh[oldEndIdx] let newEndIdx = newCh.length - 1 let newStartVnode = newCh[0] let newEndVnode = newCh[newEndIdx] let oldKeyToIdx, idxInOld, vnodeToMove, refElm while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) { if (isUndef(oldStartVnode)) { oldStartVnode = oldCh[++oldStartIdx] // Vnode has been moved left } else if (isUndef(oldEndVnode)) { oldEndVnode = oldCh[--oldEndIdx] } else if (sameVnode(oldStartVnode, newStartVnode)) { patchVnode(oldStartVnode, newStartVnode, insertedVnodeQueue) oldStartVnode = oldCh[++oldStartIdx] newStartVnode = newCh[++newStartIdx] } else if (sameVnode(oldEndVnode, newEndVnode)) { patchVnode(oldEndVnode, newEndVnode, insertedVnodeQueue) oldEndVnode = oldCh[--oldEndIdx] newEndVnode = newCh[--newEndIdx] } else if (sameVnode(oldStartVnode, newEndVnode)) { // Vnode moved right patchVnode(oldStartVnode, newEndVnode, insertedVnodeQueue) canMove && nodeOps.insertBefore(parentElm, oldStartVnode.elm, nodeOps.nextSibling(oldEndVnode.elm)) oldStartVnode = oldCh[++oldStartIdx] newEndVnode = newCh[--newEndIdx] } else if (sameVnode(oldEndVnode, newStartVnode)) { // Vnode moved left patchVnode(oldEndVnode, newStartVnode, insertedVnodeQueue) canMove && nodeOps.insertBefore(parentElm, oldEndVnode.elm, oldStartVnode.elm) oldEndVnode = oldCh[--oldEndIdx] newStartVnode = newCh[++newStartIdx] } else { if (isUndef(oldKeyToIdx)) oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx) idxInOld = isDef(newStartVnode.key) ? oldKeyToIdx[newStartVnode.key] : findIdxInOld(newStartVnode, oldCh, oldStartIdx, oldEndIdx) if (isUndef(idxInOld)) { // New element createElm(newStartVnode, insertedVnodeQueue, parentElm, oldStartVnode.elm, false, newCh, newStartIdx) } else { vnodeToMove = oldCh[idxInOld] if (sameVnode(vnodeToMove, newStartVnode)) { patchVnode(vnodeToMove, newStartVnode, insertedVnodeQueue) oldCh[idxInOld] = undefined canMove && nodeOps.insertBefore(parentElm, vnodeToMove.elm, oldStartVnode.elm) } else { // same key but different element. treat as new element createElm(newStartVnode, insertedVnodeQueue, parentElm, oldStartVnode.elm, false, newCh, newStartIdx) } } newStartVnode = newCh[++newStartIdx] } } if (oldStartIdx > oldEndIdx) { refElm = isUndef(newCh[newEndIdx + 1]) ? null : newCh[newEndIdx + 1].elm addVnodes(parentElm, refElm, newCh, newStartIdx, newEndIdx, insertedVnodeQueue) } else if (newStartIdx > newEndIdx) { removeVnodes(parentElm, oldCh, oldStartIdx, oldEndIdx) } }
一個(gè)循環(huán)比較的幾種情況和處理(以下的++ --均指index的++ --)比較則是比較的node節(jié)點(diǎn),簡(jiǎn)略寫法 不嚴(yán)謹(jǐn) 比較用的是sameVnode函數(shù)也不是真的全等
整體循環(huán)不結(jié)束的條件oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx
oldStart === newStart,oldStart++ newStart++
oldEnd === newEnd,oldEnd-- newEnd--
oldStart === newEnd, oldStart插到隊(duì)伍末尾 oldStart++ newEnd--
oldEnd === newStart, oldEnd插到隊(duì)伍開頭 oldEnd-- newStart++
剩下的所有情況都走這個(gè)處理簡(jiǎn)單的說(shuō)也就兩種處理,處理后newStart++
newStart在old中發(fā)現(xiàn)一樣的那么將這個(gè)移動(dòng)到oldStart前
沒有發(fā)現(xiàn)一樣的那么創(chuàng)建一個(gè)放到oldStart之前
循環(huán)結(jié)束后并沒有完成
還有一段判斷才算完
if (oldStartIdx > oldEndIdx) { refElm = isUndef(newCh[newEndIdx + 1]) ? null : newCh[newEndIdx + 1].elm addVnodes(parentElm, refElm, newCh, newStartIdx, newEndIdx, insertedVnodeQueue) } else if (newStartIdx > newEndIdx) { removeVnodes(parentElm, oldCh, oldStartIdx, oldEndIdx) }
簡(jiǎn)單的說(shuō)就是循環(huán)結(jié)束后,看四個(gè)指針中間的內(nèi)容,old數(shù)組中和new數(shù)組中,多退少補(bǔ)而已
以上是“vue中diff算法知識(shí)點(diǎn)有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。