欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Vue3中對VDOM的改進

 更新時間:2020年04月23日 16:40:45   作者:LhrAlander  
這篇文章主要介紹了詳解Vue3中對VDOM的改進,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

vue-next 對virtual dom的patch更新做了一系列的優(yōu)化,從編譯時加入了 block 以減少 vdom 之間的對比次數(shù),另外還有 hoisted 的操作減少了內存的開銷。本文寫給自己看,做個知識點記錄,如有錯誤,還請不吝賜教。

VDOM

VDOM的概念簡單來說就是用js對象來模擬真實DOM樹。由于MV**的架構,真實DOM樹應該隨著數(shù)據(Vue2.x中的data)的改變而發(fā)生改變,這些改變可能是以下幾個方面:

  • v-if
  • v-for
  • 動態(tài)的props(如:class,@click)
  • 子節(jié)點的改變
  • 等等

Vue框架要做的其實很單一:在用戶改變數(shù)據時,正確更新DOM樹,做法就是其核心的VDOM的patch和diff算法。

Vue2.x中的做法

在Vue2.x中,當數(shù)據改變后就要對所有的節(jié)點進行patch和diff操作。如以下DOM結構:

<div>
 <span class="header">I'm header</span>
 <ul>
  <li>第一個靜態(tài)li</li>
  <li v-for="item in mutableItems" :key="item.key"> {{ item.desc }}</li>
 </ul>
</div>

在第一次mount節(jié)點的時候會去生成真實的DOM,此后如果

mutableItems.push({
 key: 'asdf',
 desc: 'a new li item'
})

預期的結果是頁面出現(xiàn)新的一個li元素,內容就是 a new li item,Vue2.x中是通過patch時對 ul 元素對應的 vnode 的 children 來進行 diff 操作,具體操作在此不深究,但是該操作是需要比較所有的 li 對應的 vnode 的。

不足

正是由于2.x版本中的diff操作需要遍歷所有元素,本例中包括了 span 和 第一個li元素,但是這兩個元素是靜態(tài)的,不需要被比較的,不論數(shù)據怎么變,靜態(tài)元素都不會再更改了。vue-next在編譯時對這種操作做了優(yōu)化,即 Block。

Block

入上述模板,在vue-next中生成的渲染函數(shù)為:

const _Vue = Vue
const { createVNode: _createVNode } = _Vue

const _hoisted_1 = _createVNode("span", { class: "header" }, "I'm header", -1 /* HOISTED */)
const _hoisted_2 = _createVNode("li", null, "第一個靜態(tài)li", -1 /* HOISTED */)

return function render(_ctx, _cache) {
 with (_ctx) {
  const { createVNode: _createVNode, renderList: _renderList, Fragment: _Fragment, openBlock: _openBlock, createBlock: _createBlock, toDisplayString: _toDisplayString } = _Vue

  return (_openBlock(), _createBlock(_Fragment, null, [
   _hoisted_1,
   _createVNode("ul", null, [
    _hoisted_2,
    (_openBlock(true), _createBlock(_Fragment, null, _renderList(state.mutableItems, (item) => {
     return (_openBlock(), _createBlock("li", { key: item.key }, _toDisplayString(item.desc), 1 /* TEXT */))
    }), 128 /* KEYED_FRAGMENT */))
   ])
  ], 64 /* STABLE_FRAGMENT */))
 }
}

我們可以看到調用了 openBlock 和 createBlock 方法,這兩個方法的代碼實現(xiàn)也很簡單:

const blockStack: (VNode[] | null)[] = []
let currentBlock: VNode[] | null = null
let shouldTrack = 1
// openBlock
export function openBlock(disableTracking = false) {
 blockStack.push((currentBlock = disableTracking ? null : []))
}
export function createBlock(
 type: VNodeTypes | ClassComponent,
 props?: { [key: string]: any } | null,
 children?: any,
 patchFlag?: number,
 dynamicProps?: string[]
): VNode {
 // avoid a block with patchFlag tracking itself
 shouldTrack--
 const vnode = createVNode(type, props, children, patchFlag, dynamicProps)
 shouldTrack++
 // save current block children on the block vnode
 vnode.dynamicChildren = currentBlock || EMPTY_ARR
 // close block
 blockStack.pop()
 currentBlock = blockStack[blockStack.length - 1] || null
 // a block is always going to be patched, so track it as a child of its
 // parent block
 if (currentBlock) {
  currentBlock.push(vnode)
 }
 return vnode
}

更加詳細的注釋還請看源代碼中的注釋,寫的十分詳盡,便于理解。這里面 openBlock 就是初始化一個塊,createBlock 就是對當前編譯的內容生成一個塊,這里面的這一行代碼:vnode.dynamicChildren = currentBlock || EMPTY_ARR 就是在收集動態(tài)的子節(jié)點,我們可以再看一下編譯時運行的函數(shù):

// createVNode
function _createVNode(
 type: VNodeTypes | ClassComponent,
 props: (Data & VNodeProps) | null = null,
 children: unknown = null,
 patchFlag: number = 0,
 dynamicProps: string[] | null = null
) {
 /**
  * 一系列代碼
 **/

 // presence of a patch flag indicates this node needs patching on updates.
 // component nodes also should always be patched, because even if the
 // component doesn't need to update, it needs to persist the instance on to
 // the next vnode so that it can be properly unmounted later.
 if (
  shouldTrack > 0 &&
  currentBlock &&
  // the EVENTS flag is only for hydration and if it is the only flag, the
  // vnode should not be considered dynamic due to handler caching.
  patchFlag !== PatchFlags.HYDRATE_EVENTS &&
  (patchFlag > 0 ||
   shapeFlag & ShapeFlags.SUSPENSE ||
   shapeFlag & ShapeFlags.STATEFUL_COMPONENT ||
   shapeFlag & ShapeFlags.FUNCTIONAL_COMPONENT)
 ) {
  currentBlock.push(vnode)
 }
}

上述函數(shù)是在模板編譯成ast之后調用的生成VNode的函數(shù),所以有patchFlag這個標志,如果是動態(tài)的節(jié)點,并且此時是開啟了Block的話,就會將節(jié)點塞入Block中,這樣 createBlock返回的 VNode 中就會有 dynamicChildren 了。

到此為止,通過本文中案例經過模板編譯和render函數(shù)運行后并經過了優(yōu)化以后生成了如下結構的vnode:

const result = {
 type: Symbol(Fragment),
 patchFlag: 64,
 children: [
  { type: 'span', patchFlag: -1, ...},
  {
   type: 'ul',
   patchFlag: 0,
   children: [
    { type: 'li', patchFlag: -1, ...},
    {
     type: Symbol(Fragment),
     children: [
      { type: 'li', patchFlag: 1 ...},
      { type: 'li', patchFlag: 1 ...}
     ]
    }
   ]
  }
 ],
 dynamicChildren: [
  {
   type: Symbol(Fragment),
   patchFlag: 128,
   children: [
    { type: 'li', patchFlag: 1 ...},
    { type: 'li', patchFlag: 1 ...}
   ]
  }
 ]
}

以上的 result 不完整,但是我們暫時只關心這些屬性??梢钥匆?result.children 的第一個元素是span,patchFlag=-1,且 result 有一個 dynamicChildren 數(shù)組,里面只包含了兩個動態(tài)的 li,后續(xù)如果變動了數(shù)據,那么新的 vnode.dynamicChildren 會有第三個 li 元素。

patch

patch部分其實也沒差多少,就是根據vnode的type執(zhí)行不同的patch操作:

function patchElement(n1, n2) {
 let { dynamicChildren } = n2
 // 一系列操作

 if (dynamicChildren) {
  patchBlockChildren (
   n1.dynamicChildren!,
   dynamicChildren,
   el,
   parentComponent,
   parentSuspense,
   areChildrenSVG
  )
 } else if (!optimized) {
  // full diff
  patchChildren(
   n1,
   n2,
   el,
   null,
   parentComponent,
   parentSuspense,
   areChildrenSVG
  )
 }
}

可以看見,如果有了 dynamicChildren 那么vue2.x版本中的diff操作就被替換成了 patchBlockChildren() 且參數(shù)只有 dynamicChildren,就是靜態(tài)的不做diff操作了,而如果vue-next的patch中沒有 dynamicChildren,則進行完整的diff操作,入注釋寫的 full diff 的后續(xù)代碼。

結尾

本文沒有深入講解代碼的實現(xiàn)層面,一是因為自己實力不濟還在閱讀源碼當中,二是我個人認為閱讀源碼不可鉆牛角尖,從大局入眼,再徐徐圖之,先明白了各個部分的作用后帶著思考去閱讀源碼能收獲到的應該更多一些。

到此這篇關于詳解Vue3中對VDOM的改進的文章就介紹到這了,更多相關Vue3 VDOM內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • vue自定義指令添加跟隨鼠標光標提示框v-tooltip方式

    vue自定義指令添加跟隨鼠標光標提示框v-tooltip方式

    這篇文章主要介紹了vue自定義指令添加跟隨鼠標光標提示框v-tooltip方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Vue3 使用Vuex和router的注意事項及操作方法

    Vue3 使用Vuex和router的注意事項及操作方法

    在vue2中 使用的?this.$route?和?this.$router?this.$store的使用在vue3中完全適用,這篇文章主要介紹了Vue3 使用Vuex和router的注意事項及操作方法,需要的朋友可以參考下
    2022-12-12
  • vue引用public文件夾中文件的多種方式

    vue引用public文件夾中文件的多種方式

    由于一些演示需要對一些簡單頁面進行配置,由于打包build后的vue項目基本已經看不出原樣,因此需要創(chuàng)建一個文件,并在打包的時候不會進行編譯,所以文件放在public,這篇文章主要給大家介紹了關于vue引用public文件夾中文件的多種方式,需要的朋友可以參考下
    2024-02-02
  • Vue完整項目構建(進階篇)

    Vue完整項目構建(進階篇)

    這篇文章主要介紹了Vue完整項目構建(進階篇)的相關資料,需要的朋友可以參考下
    2018-02-02
  • Vue中的$set的使用實例代碼

    Vue中的$set的使用實例代碼

    這篇文章主要介紹了Vue中的$set的使用,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-10-10
  • 淺談vue.js導入css庫(elementUi)的方法

    淺談vue.js導入css庫(elementUi)的方法

    下面小編就為大家分享一篇淺談vue.js導入css庫(elementUi)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • vue3輸入框生成的時候如何自動獲取焦點詳解

    vue3輸入框生成的時候如何自動獲取焦點詳解

    記錄一下自己最近開發(fā)vue3.0的小小問題,下面這篇文章主要給大家介紹了關于vue3輸入框生成的時候如何自動獲取焦點的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-09-09
  • 詳解VUE調用本地json的使用方法

    詳解VUE調用本地json的使用方法

    這篇文章主要介紹了VUE調用本地json的使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • 解決Vue數(shù)據更新了但頁面沒有更新的問題

    解決Vue數(shù)據更新了但頁面沒有更新的問題

    在vue項目中,有些我們會遇到修改完數(shù)據,但是視圖卻沒有更新的情況,具體的場景不一樣,解決問題的方法也不一樣,在網上看了很多文章,在此總結匯總一下,需要的朋友可以參考下
    2023-08-08
  • 詳解vue+css3做交互特效的方法

    詳解vue+css3做交互特效的方法

    本篇文章主要介紹了詳解vue+css3做交互特效的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11

最新評論