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

每天學(xué)點(diǎn)Vue源碼之vm.$mount掛載函數(shù)

 更新時(shí)間:2019年03月11日 08:34:54   作者:小諾哥  
這篇文章主要介紹了vm.$mount掛載函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

在vue實(shí)例中,通過(guò)$mount()實(shí)現(xiàn)實(shí)例的掛載,下面來(lái)分析一下$mount()函數(shù)都實(shí)現(xiàn)了什么功能。

$mount函數(shù)執(zhí)行位置

_init這個(gè)私有方法是在執(zhí)行initMixin時(shí)候綁定到Vue原型上的。

 

$mount函數(shù)是如如何把組件掛在到指定元素

$mount函數(shù)定義位置

$mount函數(shù)定義位置有兩個(gè):

第一個(gè)是在src/platforms/web/runtime/index.js

這里的$mount是一個(gè)public mount method。之所以這么說(shuō)是因?yàn)閂ue有很多構(gòu)建版本, 有些版本會(huì)依賴此方法進(jìn)行有些功能定制, 后續(xù)會(huì)解釋。

// public mount method
// el: 可以是一個(gè)字符串或者Dom元素
// hydrating 是Virtual DOM 的補(bǔ)丁算法參數(shù)
Vue.prototype.$mount = function (
 el?: string | Element,
 hydrating?: boolean
): Component {
 // 判斷el, 以及宿主環(huán)境, 然后通過(guò)工具函數(shù)query重寫el。
 el = el && inBrowser ? query(el) : undefined
 // 執(zhí)行真正的掛載并返回
 return mountComponent(this, el, hydrating)
}

src/platforms/web/runtime/index.js 文件是運(yùn)行時(shí)版 Vue 的入口文件,所以這個(gè)方法是運(yùn)行時(shí)版本Vue執(zhí)行的$mount。

關(guān)于Vue不同構(gòu)建版本可以看 Vue對(duì)不同構(gòu)建版本的解釋 。

關(guān)于這個(gè)作者封裝的工具函數(shù)query也可以學(xué)習(xí)下:

/**
 * Query an element selector if it's not an element already.
 */
export function query (el: string | Element): Element {
 if (typeof el === 'string') {
  const selected = document.querySelector(el)
  if (!selected) {
   // 開發(fā)環(huán)境下給出錯(cuò)誤提示
   process.env.NODE_ENV !== 'production' && warn(
    'Cannot find element: ' + el
   )
   // 沒有找到的情況下容錯(cuò)處理
   return document.createElement('div')
  }
  return selected
 } else {
  return el
 }
}

第二個(gè)定義 $mount 函數(shù)的地方是src/platforms/web/entry-runtime-with-compiler.js 文件,這個(gè)文件是完整版Vue(運(yùn)行時(shí)+編譯器)的入口文件。

關(guān)于運(yùn)行時(shí)與編譯器不清楚的童鞋可以看官網(wǎng) 運(yùn)行時(shí) + 編譯器 vs. 只包含運(yùn)行時(shí) 。

// 緩存運(yùn)行時(shí)候定義的公共$mount方法
const mount = Vue.prototype.$mount
Vue.prototype.$mount = function (
 el?: string | Element,
 hydrating?: boolean
): Component {
 // 通過(guò)query方法重寫el(掛載點(diǎn): 組件掛載的占位符)
 el = el && query(el)

 /* istanbul ignore if */
 // 提示不能把body/html作為掛載點(diǎn), 開發(fā)環(huán)境下給出錯(cuò)誤提示
 // 因?yàn)閽燧d點(diǎn)是會(huì)被組件模板自身替換點(diǎn), 顯然body/html不能被替換
 if (el === document.body || el === document.documentElement) {
  process.env.NODE_ENV !== 'production' && warn(
   `Do not mount Vue to <html> or <body> - mount to normal elements instead.`
  )
  return this
 }
 // $options是在new Vue(options)時(shí)候_init方法內(nèi)執(zhí)行.
 // $options可以訪問(wèn)到options的所有屬性如data, filter, components, directives等
 const options = this.$options
 // resolve template/el and convert to render function
 
 // 如果包含render函數(shù)則執(zhí)行跳出,直接執(zhí)行運(yùn)行時(shí)版本的$mount方法
 if (!options.render) {
  // 沒有render函數(shù)時(shí)候優(yōu)先考慮template屬性
  let template = options.template
  if (template) {
   // template存在且template的類型是字符串
   if (typeof template === 'string') {
    if (template.charAt(0) === '#') {
     // template是ID
     template = idToTemplate(template)
     /* istanbul ignore if */
     if (process.env.NODE_ENV !== 'production' && !template) {
      warn(
       `Template element not found or is empty: ${options.template}`,
       this
      )
     }
    }
   } else if (template.nodeType) {
    // template 的類型是元素節(jié)點(diǎn),則使用該元素的 innerHTML 作為模板
    template = template.innerHTML
   } else {
    // 若 template既不是字符串又不是元素節(jié)點(diǎn),那么在開發(fā)環(huán)境會(huì)提示開發(fā)者傳遞的 template 選項(xiàng)無(wú)效
    if (process.env.NODE_ENV !== 'production') {
     warn('invalid template option:' + template, this)
    }
    return this
   }
  } else if (el) {
   // 如果template選項(xiàng)不存在,那么使用el元素的outerHTML 作為模板內(nèi)容
   template = getOuterHTML(el)
  }
  // template: 存儲(chǔ)著最終用來(lái)生成渲染函數(shù)的字符串
  if (template) {
   /* istanbul ignore if */
   if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
    mark('compile')
   }
   // 獲取轉(zhuǎn)換后的render函數(shù)與staticRenderFns,并掛在$options上
   const { render, staticRenderFns } = compileToFunctions(template, {
    outputSourceRange: process.env.NODE_ENV !== 'production',
    shouldDecodeNewlines,
    shouldDecodeNewlinesForHref,
    delimiters: options.delimiters,
    comments: options.comments
   }, this)
   options.render = render
   options.staticRenderFns = staticRenderFns

   /* istanbul ignore if */
   // 用來(lái)統(tǒng)計(jì)編譯器性能, config是全局配置對(duì)象
   if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
    mark('compile end')
    measure(`vue ${this._name} compile`, 'compile', 'compile end')
   }
  }
 }
 // 調(diào)用之前說(shuō)的公共mount方法
 // 重寫$mount方法是為了添加模板編譯的功能
 return mount.call(this, el, hydrating)
}

關(guān)于idToTemplate方法: 通過(guò)query獲取該ID獲取DOM并把該元素的innerHTML 作為模板

const idToTemplate = cached(id => {
 const el = query(id)
 return el && el.innerHTML
})

getOuterHTML方法:

/**
 * Get outerHTML of elements, taking care
 * of SVG elements in IE as well.
 */
function getOuterHTML (el: Element): string {
 if (el.outerHTML) {
  return el.outerHTML
 } else {
  // fix IE9-11 中 SVG 標(biāo)簽元素是沒有 innerHTML 和 outerHTML 這兩個(gè)屬性
  const container = document.createElement('div')
  container.appendChild(el.cloneNode(true))
  return container.innerHTML
 }
}

關(guān)于compileToFunctions函數(shù), 在src/platforms/web/entry-runtime-with-compiler.js中可以看到會(huì)掛載到Vue上作為一個(gè)全局方法。

 

mountComponent方法: 真正執(zhí)行綁定組件

mountComponent函數(shù)中是出現(xiàn)在src/core/instance/lifecycle.js。

export function mountComponent (
 vm: Component, // 組件實(shí)例vm
 el: ?Element, // 掛載點(diǎn)
 hydrating?: boolean
): Component {
 // 在組件實(shí)例對(duì)象上添加$el屬性
 // $el的值是組件模板根元素的引用
 vm.$el = el
 if (!vm.$options.render) {
  // 渲染函數(shù)不存在, 這時(shí)將會(huì)創(chuàng)建一個(gè)空的vnode對(duì)象
  vm.$options.render = createEmptyVNode
  if (process.env.NODE_ENV !== 'production') {
   /* istanbul ignore if */
   if ((vm.$options.template && vm.$options.template.charAt(0) !== '#') ||
    vm.$options.el || el) {
    warn(
     'You are using the runtime-only build of Vue where the template ' +
     'compiler is not available. Either pre-compile the templates into ' +
     'render functions, or use the compiler-included build.',
     vm
    )
   } else {
    warn(
     'Failed to mount component: template or render function not defined.',
     vm
    )
   }
  }
 }
 // 觸發(fā) beforeMount 生命周期鉤子
 callHook(vm, 'beforeMount')

 // vm._render 函數(shù)的作用是調(diào)用 vm.$options.render 函數(shù)并返回生成的虛擬節(jié)點(diǎn)(vnode)。template => render => vnode
 
 // vm._update 函數(shù)的作用是把 vm._render 函數(shù)生成的虛擬節(jié)點(diǎn)渲染成真正的 DOM。 vnode => real dom node
 
 let updateComponent // 把渲染函數(shù)生成的虛擬DOM渲染成真正的DOM
 /* istanbul ignore if */
 if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
  updateComponent = () => {
   const name = vm._name
   const id = vm._uid
   const startTag = `vue-perf-start:${id}`
   const endTag = `vue-perf-end:${id}`

   mark(startTag)
   const vnode = vm._render()
   mark(endTag)
   measure(`vue ${name} render`, startTag, endTag)

   mark(startTag)
   vm._update(vnode, hydrating)
   mark(endTag)
   measure(`vue ${name} patch`, startTag, endTag)
  }
 } else {
  updateComponent = () => {
   vm._update(vm._render(), hydrating)
  }
 }

 // we set this to vm._watcher inside the watcher's constructor
 // since the watcher's initial patch may call $forceUpdate (e.g. inside child
 // component's mounted hook), which relies on vm._watcher being already defined
 // 創(chuàng)建一個(gè)Render函數(shù)的觀察者, 關(guān)于watcher后續(xù)再講述.
 new Watcher(vm, updateComponent, noop, {
  before () {
   if (vm._isMounted && !vm._isDestroyed) {
    callHook(vm, 'beforeUpdate')
   }
  }
 }, true /* isRenderWatcher */)
 hydrating = false

 // manually mounted instance, call mounted on self
 // mounted is called for render-created child components in its inserted hook
 if (vm.$vnode == null) {
  vm._isMounted = true
  callHook(vm, 'mounted')
 }
 return vm
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue集成Iframe頁(yè)面的方法示例

    Vue集成Iframe頁(yè)面的方法示例

    這篇文章主要介紹了Vue集成Iframe頁(yè)面的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • 解決vuex數(shù)據(jù)異步造成初始化的時(shí)候沒值報(bào)錯(cuò)問(wèn)題

    解決vuex數(shù)據(jù)異步造成初始化的時(shí)候沒值報(bào)錯(cuò)問(wèn)題

    今天小編大家分享一篇解決vuex數(shù)據(jù)異步造成初始化的時(shí)候沒值報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • 詳解如何在Vue3中捕獲和處理錯(cuò)誤

    詳解如何在Vue3中捕獲和處理錯(cuò)誤

    Vue 3 作為前端開發(fā)中一個(gè)非常流行的框架,在錯(cuò)誤處理方面提供了靈活和強(qiáng)大的能力,本文將深入介紹在 Vue 3 中如何捕獲和處理錯(cuò)誤,包括組件級(jí)的錯(cuò)誤處理、全局錯(cuò)誤處理以及如何與異常日志系統(tǒng)集成,需要的朋友可以參考下
    2024-07-07
  • vuex存儲(chǔ)數(shù)組(新建,增,刪,更新)并存入localstorage定時(shí)刪除功能實(shí)現(xiàn)

    vuex存儲(chǔ)數(shù)組(新建,增,刪,更新)并存入localstorage定時(shí)刪除功能實(shí)現(xiàn)

    這篇文章主要介紹了vuex存儲(chǔ)數(shù)組(新建,增,刪,更新),并存入localstorage定時(shí)刪除,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • Vue之Axios的異步請(qǐng)求問(wèn)題詳解

    Vue之Axios的異步請(qǐng)求問(wèn)題詳解

    總的來(lái)說(shuō)這并不是一道難題,那為什么要拿出這道題介紹?拿出這道題真正想要傳達(dá)的是解題的思路,以及不斷優(yōu)化探尋最優(yōu)解的過(guò)程。希望通過(guò)這道題能給你帶來(lái)一種解題優(yōu)化的思路,Axios是一個(gè)開源的可以用在瀏覽器端和Node JS的異步通信框架,主要作用就是實(shí)現(xiàn)AJAX異步通信
    2023-02-02
  • 在vue項(xiàng)目中 實(shí)現(xiàn)定義全局變量 全局函數(shù)操作

    在vue項(xiàng)目中 實(shí)現(xiàn)定義全局變量 全局函數(shù)操作

    這篇文章主要介紹了在vue項(xiàng)目中 實(shí)現(xiàn)定義全局變量 全局函數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • 解決$store.getters調(diào)用不執(zhí)行的問(wèn)題

    解決$store.getters調(diào)用不執(zhí)行的問(wèn)題

    今天小編就為大家分享一篇解決$store.getters調(diào)用不執(zhí)行的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • 關(guān)于vue3?option?api新玩法分享

    關(guān)于vue3?option?api新玩法分享

    vue3新特性中最重要、內(nèi)容最多的組合式api,組合式api既可以解決之前vue2開發(fā)的痛點(diǎn),又提升了性能,下面這篇文章主要給大家介紹了關(guān)于vue3?option?api新玩法的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • vue使用echarts圖表的詳細(xì)方法

    vue使用echarts圖表的詳細(xì)方法

    這篇文章主要為大家詳細(xì)介紹了vue使用echarts圖表的詳細(xì)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • 如何在vue 中引入使用jquery

    如何在vue 中引入使用jquery

    這篇文章主要介紹了如何在vue 中引入使用jquery,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2020-11-11

最新評(píng)論