自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Vue 3.0 進階之應用創(chuàng)建的過程

開發(fā) 前端
在這篇文章中,阿寶哥將帶大家一起探索 Vue 3 中應用創(chuàng)建的過程。接下來,我們將從一個簡單的例子出發(fā),從頭開始一步步分析 Vue 3.0 應用創(chuàng)建的過程。

[[385333]]

本文轉(zhuǎn)載自微信公眾號「全棧修仙之路」,作者阿寶哥。轉(zhuǎn)載本文請聯(lián)系全棧修仙之路公眾號。

本文是 Vue 3.0 進階系列 的第七篇文章,在這篇文章中,阿寶哥將帶大家一起探索 Vue 3 中應用創(chuàng)建的過程。接下來,我們將從一個簡單的例子出發(fā),從頭開始一步步分析 Vue 3.0 應用創(chuàng)建的過程。

  1. <div id="app"></div> 
  2. <script> 
  3.    const { createApp, h } = Vue 
  4.    const app = createApp({ // ① 
  5.      data() { 
  6.        return { 
  7.          name'我是阿寶哥' 
  8.        } 
  9.      }, 
  10.      template: `<div>大家好, {{name}}!</div>` 
  11.    }) 
  12.    app.mount('#app') // ② 
  13. </script> 

 

 

 

在以上代碼中,首先我們通過 createApp 函數(shù)創(chuàng)建 app 對象,然后調(diào)用 app.mount 方法執(zhí)行應用掛載操作。當以上代碼成功運行后,頁面上會顯示 大家好,我是阿寶哥!,具體如下圖所示:

 

對于以上的示例來說,它主要包含兩個步驟:創(chuàng)建 app 對象和應用掛載。這里我們只分析創(chuàng)建 app 對象的過程,而應用掛載的過程將在下一篇文章中介紹。

一、創(chuàng)建 app 對象

首先,阿寶哥利用 Chrome 開發(fā)者工具的 Performance 標簽欄,記錄了創(chuàng)建 app 對象的主要過程:

 

從圖中我們看到了在創(chuàng)建 app 對象過程中,所涉及的相關(guān)函數(shù)。為了讓大家能直觀地了解 app 對象創(chuàng)建的過程,阿寶哥畫了一張圖:

 

大致了解了主要過程之后,我們從 createApp 這個入口開始分析。接下來,打開 Chrome 開發(fā)者工具,在 createApp 處加個斷點:

 

通過斷點,我們找到了 createApp 函數(shù),調(diào)用該函數(shù)之后會返回一個提供應用上下文的應用實例。應用實例掛載的整個組件樹共享同一個上下文。createApp 函數(shù)被定義在 runtime-dom/src/index.ts 文件中:

 

  1. // packages/runtime-dom/src/index.ts 
  2. export const createApp = ((...args) => { 
  3.   const app = ensureRenderer().createApp(...args) 
  4.  
  5.   const { mount } = app 
  6.   app.mount = (containerOrSelector: Element | ShadowRoot | string): any => { 
  7.     // 省略mount內(nèi)部的處理邏輯 
  8.   } 
  9.   return app 
  10. }) as CreateAppFunction<Element> 

在 createApp 內(nèi)部,會先調(diào)用 ensureRenderer 函數(shù),該函數(shù)的內(nèi)部代碼很簡單:

  1. // packages/runtime-dom/src/index.ts 
  2. function ensureRenderer() { 
  3.   return renderer || (renderer = createRenderer<Node, Element>(rendererOptions)) 

在以上代碼中會延遲創(chuàng)建渲染器,那么為什么要這樣做呢?我們從 runtime-dom/src/index.ts 文件中的注釋,找到了答案:

  1. // lazy create the renderer - this makes core renderer logic tree-shakable 
  2. // in case the user only imports reactivity utilities from Vue. 

對于我們的示例來說,是需要使用到渲染器的,所以會調(diào)用 createRenderer 函數(shù)創(chuàng)建渲染器。在分析 createRenderer 函數(shù)前,我們先來分析一下它的參數(shù)rendererOptions:

  1. // packages/runtime-dom/src/index.ts 
  2. export const extend = Object.assign 
  3. const rendererOptions = extend({ patchProp, forcePatchProp }, nodeOps) 

由以上代碼可知,參數(shù) rendererOptions 是一個包含 patchProp、forcePatchProp 等屬性的對象,其中 nodeOps 是 node operations 的縮寫。對于 Web 瀏覽器環(huán)境來說,它定義了操作節(jié)點/元素的 API,比如創(chuàng)建元素、創(chuàng)建文本節(jié)點、插入元素和刪除元素等。因為 Vue 3.0 的源碼是使用 TypeScript 編寫的,所以可以在源碼中找到rendererOptions 參數(shù)的類型定義:

  1. // packages/runtime-core/src/renderer.ts 
  2. export interface RendererOptions< 
  3.   HostNode = RendererNode, 
  4.   HostElement = RendererElement 
  5. > { 
  6.   patchProp(el: HostElement, key: string, prevValue: any, nextValue: any, ...): void 
  7.   forcePatchProp?(el: HostElement, key: string): boolean 
  8.   insert(el: HostNode, parent: HostElement, anchor?: HostNode | null): void 
  9.   remove(el: HostNode): void 
  10.   createElement( type: string, isSVG?: boolean, isCustomizedBuiltIn?: string): HostElement 
  11.   createText(text: string): HostNode 
  12.   createComment(text: string): HostNode 
  13.   setText(node: HostNode, text: string): void 
  14.   setElementText(node: HostElement, text: string): void 
  15.   parentNode(node: HostNode): HostElement | null 
  16.   nextSibling(node: HostNode): HostNode | null 
  17.   querySelector?(selector: string): HostElement | null 
  18.   setScopeId?(el: HostElement, id: string): void 
  19.   cloneNode?(node: HostNode): HostNode 
  20.   insertStaticContent?(content: string, parent: HostElement, ...): HostElement[] 

在 RendererOptions 接口中定義了與渲染器相關(guān)的所有方法,這樣做的目的是對渲染器做了一層抽象。開發(fā)者在滿足該接口約束的情況下,就可以根據(jù)自己的需求實現(xiàn)自定義渲染器。了解完 rendererOptions 參數(shù),我們來介紹 createRenderer 函數(shù):

  1. // packages/runtime-core/src/renderer.ts 
  2. export interface RendererNode { 
  3.   [key: string]: any  // 索引簽名 
  4. export interface RendererElement extends RendererNode {} 
  5.  
  6. export function createRenderer< 
  7.   HostNode = RendererNode, 
  8.   HostElement = RendererElement 
  9. >(options: RendererOptions<HostNode, HostElement>) { 
  10.   return baseCreateRenderer<HostNode, HostElement>(options) 

在 createRenderer 函數(shù)內(nèi)部會繼續(xù)調(diào)用 baseCreateRenderer 函數(shù)來執(zhí)行創(chuàng)建渲染器的邏輯,該函數(shù)內(nèi)部的邏輯比較復雜,這里我們先來看一下調(diào)用該函數(shù)后的返回結(jié)果:

  1. // packages/runtime-core/src/renderer.ts 
  2. function baseCreateRenderer( 
  3.   options: RendererOptions, 
  4.   createHydrationFns?: typeof createHydrationFunctions 
  5. ): any { 
  6.   // 省略大部分代碼 
  7.   return { 
  8.     render, 
  9.     hydrate, 
  10.     createApp: createAppAPI(render, hydrate) 
  11.   } 

在以上代碼中,我們終于看到了期待已久的 createApp 屬性,該屬性的值是調(diào)用 createAppAPI 函數(shù)后的返回結(jié)果??催^阿寶哥之前文章的小伙伴,對 createAppAPI 函數(shù)應該不會陌生,它被定義在 runtime-core/src/apiCreateApp.ts 文件中:

  1. // packages/runtime-core/src/apiCreateApp.ts 
  2. export function createAppAPI<HostElement>( 
  3.   render: RootRenderFunction, 
  4.   hydrate?: RootHydrateFunction 
  5. ): CreateAppFunction<HostElement> { 
  6.   return function createApp(rootComponent, rootProps = null) { 
  7.     const context = createAppContext() 
  8.     const installedPlugins = new Set() 
  9.     let isMounted = false 
  10.     const app: App = (context.app = { 
  11.       _uid: uid++, 
  12.       _component: rootComponent as ConcreteComponent, 
  13.       _context: context, 
  14.       // 省略use、mixin、unmount和provide等方法 
  15.       component(name: string, component?: Component): any { 
  16.      // ... 
  17.       }, 
  18.       directive(name: string, directive?: Directive) { 
  19.         // ... 
  20.       }, 
  21.       mount(rootContainer: HostElement, isHydrate?: boolean): any { 
  22.         // ... 
  23.       }, 
  24.     }) 
  25.     return app 
  26.   } 

通過以上的代碼可知,createApp 方法支持 rootComponent 和 rootProps 兩個參數(shù),調(diào)用該方法之后會返回一個 app 對象,該對象為了開發(fā)者提供了多個應用 API,比如,用于注冊或檢索全局組件的 component 方法,用于注冊或檢索全局指令的 directive方法及用于將應用實例的根組件掛載到指定 DOM 元素上的 mount 方法等。

此外,在 createApp 函數(shù)體中,我們看到了 const context = createAppContext() 這行代碼。顧名思義,createAppContext 函數(shù)用于創(chuàng)建與當前應用相關(guān)的上下文對象。那么所謂的上下文對象長啥樣呢?要搞清楚這個問題,我們來看一下 createAppContext 函數(shù)的具體實現(xiàn):

  1. // packages/runtime-core/src/apiCreateApp.ts 
  2. export function createAppContext(): AppContext { 
  3.   return { 
  4.     app: null as any
  5.     config: { ... }, 
  6.     mixins: [], 
  7.     components: {}, 
  8.     directives: {}, 
  9.     provides: Object.create(null
  10.   } 

介紹完 app 和 context 對象之后,我們來繼續(xù)分析 createApp 函數(shù)剩下的邏輯代碼:

 

  1. // packages/runtime-dom/src/index.ts 
  2. export const createApp = ((...args) => { 
  3.   const app = ensureRenderer().createApp(...args) 
  4.  
  5.   const { mount } = app 
  6.   app.mount = (containerOrSelector: Element | ShadowRoot | string): any => { 
  7.     // 省略mount內(nèi)部的處理邏輯 
  8.   } 
  9.   return app 
  10. }) as CreateAppFunction<Element> 

由以上代碼可知,在創(chuàng)建完 app 對象之后,并不會立即返回已創(chuàng)建的 app 對象,而是會重寫 app.mount 屬性:

 

  1. // packages/runtime-dom/src/index.ts 
  2. export const createApp = ((...args) => { 
  3.   const app = ensureRenderer().createApp(...args) 
  4.  
  5.   const { mount } = app 
  6.   app.mount = (containerOrSelector: Element | ShadowRoot | string): any => { 
  7.     const container = normalizeContainer(containerOrSelector) // 同時支持字符串和DOM對象 
  8.     if (!container) return 
  9.     const component = app._component 
  10.     // 若根組件非函數(shù)對象且未設置render和template屬性,則使用容器的innerHTML作為模板的內(nèi)容 
  11.     if (!isFunction(component) && !component.render && !component.template) { 
  12.       component.template = container.innerHTML 
  13.     } 
  14.     container.innerHTML = '' // 在掛載前清空容器內(nèi)容 
  15.     const proxy = mount(container) // 執(zhí)行掛載操作 
  16.     if (container instanceof Element) { 
  17.       container.removeAttribute('v-cloak') // 避免在網(wǎng)絡不好或加載數(shù)據(jù)過大的情況下,頁面渲染的過程中會出現(xiàn)Mustache標簽 
  18.       container.setAttribute('data-v-app'''
  19.     } 
  20.     return proxy 
  21.   } 
  22.  
  23.   return app 
  24. }) as CreateAppFunction<Element> 

在 app.mount 方法內(nèi)部,當設置好根組件的相關(guān)信息之后,就會調(diào)用 app 對象原始的mount 方法執(zhí)行掛載操作:

  1. // packages/runtime-core/src/apiCreateApp.ts 
  2. export function createAppAPI<HostElement>( 
  3.   render: RootRenderFunction, 
  4.   hydrate?: RootHydrateFunction 
  5. ): CreateAppFunction<HostElement> { 
  6.   return function createApp(rootComponent, rootProps = null) { 
  7.     const context = createAppContext() 
  8.     const installedPlugins = new Set() 
  9.  
  10.     let isMounted = false // 標識是否已掛載 
  11.  
  12.     const app: App = (context.app = { 
  13.       _uid: uid++, 
  14.       _component: rootComponent as ConcreteComponent, 
  15.       _props: rootProps, 
  16.       _context: context, 
  17.  
  18.       mount(rootContainer: HostElement, isHydrate?: boolean): any { 
  19.         if (!isMounted) { 
  20.           // 基于根組件和根組件屬性創(chuàng)建對應的VNode節(jié)點 
  21.           const vnode = createVNode( 
  22.             rootComponent as ConcreteComponent, 
  23.             rootProps 
  24.           ) 
  25.           vnode.appContext = context // 應用上下文 
  26.           if (isHydrate && hydrate) { // 與服務端渲染相關(guān) 
  27.             hydrate(vnode as VNode<Node, Element>, rootContainer as any
  28.           } else { // 把vnode渲染到根容器中 
  29.             render(vnode, rootContainer) 
  30.           } 
  31.           isMounted = true // 設置已掛載的狀態(tài)  
  32.           app._container = rootContainer 
  33.           return vnode.component!.proxy 
  34.         } 
  35.       }, 
  36.     }) 
  37.  
  38.     return app 
  39.   } 

那么為什么要重寫 app.mount 方法呢?原因是為了支持跨平臺,在 runtime-dom 包中定義的 app.mount 方法,都是與 Web 平臺有關(guān)的方法。另外,在 runtime-dom 包中,還會為 Web 平臺創(chuàng)建該平臺對應的渲染器。即在創(chuàng)建渲染器時,使用的 nodeOps 對象中封裝了 DOM 相關(guān)的 API:

  1. // packages/runtime-dom/src/nodeOps.ts 
  2. export const nodeOps: Omit<RendererOptions<Node, Element>, 'patchProp'> = { 
  3.   // 省略部分方法 
  4.   createElement: (tag, isSVG, is): Element => 
  5.     isSVG ? doc.createElementNS(svgNS, tag) : doc.createElement(tag, is ? { is } : undefined), 
  6.   createText: text => doc.createTextNode(text), 
  7.   createComment: text => doc.createComment(text), 
  8.   querySelector: selector => doc.querySelector(selector), 

現(xiàn)在創(chuàng)建 app 對象的過程中涉及的主要函數(shù)已經(jīng)介紹完了,對這個過程還不理解的小伙伴,可以參考阿寶哥前面畫的圖,然后斷點調(diào)試一下創(chuàng)建 app 對象的過程。

 

二、阿寶哥有話說

2.1 App 對象提供哪些 API?

在 Vue 3 中,改變?nèi)?Vue 行為的 API 現(xiàn)在被移動到了由新的 createApp 方法所創(chuàng)建的應用實例上。應用實例為我們提供了以下 API 來實現(xiàn)特定的功能:

  • config():包含應用配置的對象。
  • unmount():在提供的 DOM 元素上卸載應用實例的根組件。
  • mixin(mixin: ComponentOptions):將一個 mixin 應用在整個應用范圍內(nèi)。
  • provide(key, value):設置一個可以被注入到應用范圍內(nèi)所有組件中的值。
  • component(name: string, component?: Component):注冊或檢索全局組件。
  • directive(name: string, directive?: Directive):注冊或檢索全局指令。
  • use(plugin: Plugin, ...options: any[]):安裝 Vue.js 插件,當在同一個插件上多次調(diào)用此方法時,該插件將僅安裝一次。
  • mount(rootContainer: HostElement, isHydrate?: boolean,isSVG?: boolean):將應用實例的根組件掛載在提供的 DOM 元素上。

2.2 使用 createApp 函數(shù)可以創(chuàng)建多個 Vue 應用么?

通過 createApp 函數(shù),我們可以輕松地創(chuàng)建多個 Vue 應用。每個應用的上下文環(huán)境都是互相隔離的,具體的使用方式如下所示:

  1. <div id="appA"></div> 
  2. <hr> 
  3. <div id="appB"></div> 
  4. <script> 
  5.   const { createApp, h } = Vue 
  6.   const appA = createApp({ 
  7.     template: "我是應用A" 
  8.   }) 
  9.   const appB = createApp({ 
  10.     template: "我是應用B" 
  11.   }) 
  12.   appA.mount('#appA')  
  13.   appB.mount('#appB')  
  14. </script> 

本文主要介紹了在 Vue 3 中創(chuàng)建 App 對象的主要過程及 App 對象上相關(guān)的 API。為了讓大家能夠更深入地理解 App 對象創(chuàng)建的過程,阿寶哥還從源碼的角度分析了該過程中涉及的主要函數(shù)。在下一篇文章中,阿寶哥將會介紹應用掛載的過程,感興趣的小伙伴不要錯過喲。

三、參考資源

 

Vue 3 官網(wǎng) - 全局 API

 

責任編輯:武曉燕 來源: 全棧修仙之路
相關(guān)推薦

2021-03-08 00:08:29

Vue應用掛載

2021-02-16 16:41:45

Vue項目指令

2021-02-26 05:19:20

Vue 3.0 VNode虛擬

2021-02-28 20:41:18

Vue注入Angular

2021-02-19 23:07:02

Vue綁定組件

2021-02-22 21:49:33

Vue動態(tài)組件

2021-02-18 08:19:21

Vue自定義Vue 3.0

2021-03-09 22:29:46

Vue 響應式API

2011-07-19 15:18:46

存儲過程sql語句

2020-10-13 08:24:31

Vue3.0系列

2011-06-23 15:10:39

Qt 窗體

2021-04-26 18:48:48

微應用React

2017-02-27 11:48:58

JVM源碼分析Java

2009-10-22 14:05:55

CLR存儲過程

2010-04-23 11:15:43

Oracle創(chuàng)建

2020-11-02 11:33:52

ReactVue應用

2020-04-22 14:15:32

Vue 3.0語法前端

2011-08-17 16:16:29

iPhone應用程序啟動過程

2018-11-05 15:14:42

MySQL編程語言技術(shù)

2020-09-16 06:12:30

Vue.js 3.0Suspense組件前端
點贊
收藏

51CTO技術(shù)棧公眾號