頁面初始化的所有狀態都準備就緒之后,下一步就是要生成組件相應的虛擬節點—— VNode 。初次進行組件初始化的時候, VNode 也會執行一次初始化并存儲這時創建好的虛擬節點對象。在隨后的生命周期中,組件內的數據發生變動時,會先生成新的 VNode 對象,然后再根據與之前存儲的舊虛擬節點的對比來執行刷新頁面 DOM 的操作。頁面刷新的流程大致上可以這樣簡單的總結,但是其實現路程是非常復雜的,為了深入地了解虛擬節點生成和更新的過程,首先來看看 VNode 類的具體實現。
VNode 類
VNode 類的實現是支持頁面渲染的基礎,這個類的實現并不復雜,但無論是創建Vue組件實例還是使用動態JS擴展函數組件都運用到了渲染函數 render ,它充分利用了 VNode 來構建虛擬DOM樹。
// 定義并導出VNode類 export default class VNode { // 定義實例屬性 tag: string | void; // 標簽名稱 data: VNodeData | void; // 節點數據 children: ?Array<VNode>; // 子虛擬節點列表 text: string | void; // 節點文字 elm: Node | void; // 對應DOM節點 ns: string | void; // 節點命名空間,針對svg標簽的屬性 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 // 是否包含原始HTML。只有服務器端會使用 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 // 方法作用域id 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 } // 定義child屬性的取值器 // 已棄用:用于向后compat的componentInstance的別名 // DEPRECATED: alias for componentInstance for backwards compat. /* istanbul ignore next */ get child (): Component | void { return this.componentInstance } } // 定義并導出createEmptyVNode函數,創建空虛擬節點 export const createEmptyVNode = (text: string = '') => { // 實例化虛擬節點 const node = new VNode() // 設置節點文字為空,并設置為注釋節點 node.text = text node.isComment = true // 返回節點 return node } // 定義并導出createTextVNode函數,創建文字虛擬節點 export function createTextVNode (val: string | number) { // 置空實例初始化的標簽名,數據,子節點屬性,只傳入文字 return new VNode(undefined, undefined, undefined, String(val)) } // 優化淺拷貝 // 用于靜態節點和插槽節點,因為它們可以在多個渲染中重用, // 當DOM操作依賴于它們的elm引用時,克隆它們可以避免錯誤 // optimized shallow clone // used for static nodes and slot nodes because they may be reused across // multiple renders, cloning them avoids errors when DOM manipulations rely // on their elm reference. // 定義并導出cloneVNode函數,拷貝節點 export function cloneVNode (vnode: VNode): VNode { // 拷貝節點并返回 const cloned = new VNode( vnode.tag, vnode.data, vnode.children, vnode.text, vnode.elm, vnode.context, vnode.componentOptions, vnode.asyncFactory ) cloned.ns = vnode.ns cloned.isStatic = vnode.isStatic cloned.key = vnode.key cloned.isComment = vnode.isComment cloned.fnContext = vnode.fnContext cloned.fnOptions = vnode.fnOptions cloned.fnScopeId = vnode.fnScopeId cloned.asyncMeta = vnode.asyncMeta cloned.isCloned = true return cloned }
VNode 類實現的源代碼分兩部分,第一部分是定義 VNode 類自身的實現,第二部分是定一些常用的節點創建方法,包括創建空的虛擬節點,文字虛擬節點和新拷貝節點。虛擬節點本身是一個包含了所有渲染所需信息的載體,從前面一部分的屬性就可以看出,不僅有相應的 DOM 標簽和屬性信息,還包含了子虛擬節點列表,所以一個組件初始化之后得到的 VNode 也是一棵虛擬節點樹,實質是抽象和信息化了的對應于 DOM 樹的 JS 對象。
VNode 的使用在服務器渲染中也有應用,關于這一部分暫時放到之后去研究。
認識到 VNode 的實質之后,對于它的基礎性的作用還是不太清楚,為什么需要創建這種對象來呢?答案就在Vue的響應式刷新里。如前所述,觀察系統實現了對數據變更的監視,在收到變更的通知之后處理權就移交到渲染系統手上,渲染系統首先進行的處理就是根據變動生成新虛擬節點樹,然后再去對比舊的虛擬節點樹,來實現這個抽象對象的更新,簡單的來說就是通過新舊兩個節點樹的對照,來最終確定一個真實DOM建立起來所需要依賴的抽象對象,只要這個真實 DOM 所依賴的對象確定好,渲染函數會把它轉化成真實的 DOM 樹。
最后來概括地描述一下 VNode 渲染成真實 DOM 的路徑:
渲染路徑
Vue 的一般渲染有兩條路徑:
在研究生命周期的時候知道,有 mount 和 update 兩個鉤子函數,這兩個生命周期的過程分別代表了兩條渲染路徑的執行。
組件實例初始創建生成DOM
Vue 組件實例初始創建時,走的是 mount 這條路徑,在這條路徑上初始沒有已暫存的舊虛擬節點,要經歷第一輪 VNode 的生成。這一段代碼的執行是從 $mount 函數開始的:
$mount => mountComponent => updateComponent => _render => _update => createPatchFunction(patch) => createElm => insert => removeVnodes
大致描述一下每一個流程中所進行的關于節點的處理:
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com