vue实例没有挂载到html上,Vue 实例挂载的实现

new Vue 发生了什么

new 关键字在 Javascript 语言中代表实例化是一个对象,而 Vue 实际上是一个类,类在 Javascript 中是用 Function 来实现的

源码实现,在src/core/instance/index.js 中

function Vue (options) {

if (process.env.NODE_ENV !== 'production' &&

!(this instanceof Vue)

) {

warn('Vue is a constructor and should be called with the `new` keyword')

}

this._init(options)

}

可以看到 Vue 只能通过 new 关键字初始化,然后会调用 this._init 方法, 该方法在 src/core/instance/init.js 中定义。

Vue.prototype._init = function (options?: Object) {

const vm: Component = this

// a uid

vm._uid = uid++

let startTag, endTag

/* istanbul ignore if */

if (process.env.NODE_ENV !== 'production' && config.performance && mark) {

startTag = `vue-perf-start:${vm._uid}`

endTag = `vue-perf-end:${vm._uid}`

mark(startTag)

}

// a flag to avoid this being observed

vm._isVue = true

// merge options

if (options && options._isComponent) {

// optimize internal component instantiation

// since dynamic options merging is pretty slow, and none of the

// internal component options needs special treatment.

initInternalComponent(vm, options)

} else {

vm.$options = mergeOptions(

resolveConstructorOptions(vm.constructor),

options || {},

vm

)

}

/* istanbul ignore else */

if (process.env.NODE_ENV !== 'production') {

initProxy(vm)

} else {

vm._renderProxy = vm

}

// expose real self

vm._self = vm

initLifecycle(vm)

initEvents(vm)

initRender(vm)

callHook(vm, 'beforeCreate')

initInjections(vm) // resolve injections before data/props

initState(vm)

initProvide(vm) // resolve provide after data/props

callHook(vm, 'created')

/* istanbul ignore if */

if (process.env.NODE_ENV !== 'production' && config.performance && mark) {

vm._name = formatComponentName(vm, false)

mark(endTag)

measure(`vue ${vm._name} init`, startTag, endTag)

}

if (vm.$options.el) {

vm.$mount(vm.$options.el)

}

}

Vue 初始化主要就干了几件事情,合并配置,初始化生命周期,初始化事件中心,初始化渲染,初始化 data、props、computed、watcher 等等。

Vue 的初始化逻辑写的非常清楚,把不同的功能逻辑拆成一些单独的函数执行。

在初始化的最后,检测到如果有 el 属性,则调用 vm.$mount 方法挂载 vm,挂载的目标就是把模板渲染成最终的 DOM。

Vue 的挂载过程

Vue 中是通过

math?formula=mount%20%E5%AE%9E%E4%BE%8B%E6%96%B9%E6%B3%95%E5%8E%BB%E6%8C%82%E8%BD%BD%20vm%20%E7%9A%84%EF%BC%8Cmount 方法在多个文件中都有定义,如 src/platform/web/entry-runtime-with-compiler.js、src/platform/web/runtime/index.js、src/platform/weex/runtime/index.js。因为

math?formula=mount%20%E8%BF%99%E4%B8%AA%E6%96%B9%E6%B3%95%E7%9A%84%E5%AE%9E%E7%8E%B0%E6%98%AF%E5%92%8C%E5%B9%B3%E5%8F%B0%E3%80%81%E6%9E%84%E5%BB%BA%E6%96%B9%E5%BC%8F%E9%83%BD%E7%9B%B8%E5%85%B3%E7%9A%84%E3%80%82%E9%87%8D%E7%82%B9%E5%88%86%E6%9E%90%E5%B8%A6%20compiler%20%E7%89%88%E6%9C%AC%E7%9A%84mount 实现,因为抛开 webpack 的 vue-loader,在纯前端浏览器环境分析 Vue 的工作原理,有助于我们对原理理解的深入。

compiler 版本的 $mount在src/platform/web/entry-runtime-with-compiler.js 文件中定义:

const mount = Vue.prototype.$mount

Vue.prototype.$mount = function (

el?: string | Element,

hydrating?: boolean

): Component {

el = el && query(el)

/* istanbul ignore if */

if (el === document.body || el === document.documentElement) {

process.env.NODE_ENV !== 'production' && warn(

`Do not mount Vue to or

- mount to normal elements instead.`

)

return this

}

const options = this.$options

// resolve template/el and convert to render function

if (!options.render) {

let template = options.template

if (template) {

if (typeof template === 'string') {

if (template.charAt(0) === '#') {

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 = template.innerHTML

} else {

if (process.env.NODE_ENV !== 'production') {

warn('invalid template option:' + template, this)

}

return this

}

} else if (el) {

template = getOuterHTML(el)

}

if (template) {

/* istanbul ignore if */

if (process.env.NODE_ENV !== 'production' && config.performance && mark) {

mark('compile')

}

const { render, staticRenderFns } = compileToFunctions(template, {

shouldDecodeNewlines,

shouldDecodeNewlinesForHref,

delimiters: options.delimiters,

comments: options.comments

}, this)

options.render = render

options.staticRenderFns = staticRenderFns

/* istanbul ignore if */

if (process.env.NODE_ENV !== 'production' && config.performance && mark) {

mark('compile end')

measure(`vue ${this._name} compile`, 'compile', 'compile end')

}

}

}

return mount.call(this, el, hydrating)

}

这段代码首先缓存了原型上的'

math?formula=mount'%20%E6%96%B9%E6%B3%95%EF%BC%8C%E5%86%8D%E9%87%8D%E6%96%B0%E5%AE%9A%E4%B9%89%E8%AF%A5%E6%96%B9%E6%B3%95%E3%80%82%E9%A6%96%E5%85%88%EF%BC%8C%E5%AE%83%E5%AF%B9%20el%20%E5%81%9A%E4%BA%86%E9%99%90%E5%88%B6%EF%BC%8CVue%20%E4%B8%8D%E8%83%BD%E6%8C%82%E8%BD%BD%E5%9C%A8%20body%E3%80%81html%20%E8%BF%99%E6%A0%B7%E7%9A%84%E6%A0%B9%E8%8A%82%E7%82%B9%E4%B8%8A%E3%80%82%E6%8E%A5%E4%B8%8B%E6%9D%A5%E7%9A%84%E6%98%AF%E5%BE%88%E5%85%B3%E9%94%AE%E7%9A%84%E9%80%BB%E8%BE%91%20%E2%80%94%E2%80%94%20%E5%A6%82%E6%9E%9C%E6%B2%A1%E6%9C%89%E5%AE%9A%E4%B9%89%20render%20%E6%96%B9%E6%B3%95%EF%BC%8C%E5%88%99%E4%BC%9A%E6%8A%8A%20el%20%E6%88%96%E8%80%85%20template%20%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E6%8D%A2%E6%88%90%20render%20%E6%96%B9%E6%B3%95%E3%80%82%E8%BF%99%E9%87%8C%E6%88%91%E4%BB%AC%E8%A6%81%E7%89%A2%E8%AE%B0%EF%BC%8C%E5%9C%A8%20Vue%202.0%20%E7%89%88%E6%9C%AC%E4%B8%AD%EF%BC%8C%E6%89%80%E6%9C%89%20Vue%20%E7%9A%84%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%93%E6%9C%80%E7%BB%88%E9%83%BD%E9%9C%80%E8%A6%81%20render%20%E6%96%B9%E6%B3%95%EF%BC%8C%E6%97%A0%E8%AE%BA%E6%88%91%E4%BB%AC%E6%98%AF%E7%94%A8%E5%8D%95%E6%96%87%E4%BB%B6%20.vue%20%E6%96%B9%E5%BC%8F%E5%BC%80%E5%8F%91%E7%BB%84%E4%BB%B6%EF%BC%8C%E8%BF%98%E6%98%AF%E5%86%99%E4%BA%86%20el%20%E6%88%96%E8%80%85%20template%20%E5%B1%9E%E6%80%A7%EF%BC%8C%E6%9C%80%E7%BB%88%E9%83%BD%E4%BC%9A%E8%BD%AC%E6%8D%A2%E6%88%90%20render%20%E6%96%B9%E6%B3%95%EF%BC%8C%E9%82%A3%E4%B9%88%E8%BF%99%E4%B8%AA%E8%BF%87%E7%A8%8B%E6%98%AF%20Vue%20%E7%9A%84%E4%B8%80%E4%B8%AA%E2%80%9C%E5%9C%A8%E7%BA%BF%E7%BC%96%E8%AF%91%E2%80%9D%E7%9A%84%E8%BF%87%E7%A8%8B%EF%BC%8C%E5%AE%83%E6%98%AF%E8%B0%83%E7%94%A8%20compileToFunctions%20%E6%96%B9%E6%B3%95%E5%AE%9E%E7%8E%B0%E7%9A%84%EF%BC%8C%E7%BC%96%E8%AF%91%E8%BF%87%E7%A8%8B%E6%88%91%E4%BB%AC%E4%B9%8B%E5%90%8E%E4%BC%9A%E4%BB%8B%E7%BB%8D%E3%80%82%E6%9C%80%E5%90%8E%EF%BC%8C%E8%B0%83%E7%94%A8%E5%8E%9F%E5%85%88%E5%8E%9F%E5%9E%8B%E4%B8%8A%E7%9A%84mount 方法挂载。

原先原型上的 $mount 方法在 src/platform/web/runtime/index.js 中定义,之所以这么设计完全是为了复用,因为它是可以被 runtime only 版本的 Vue 直接使用的。

Vue.prototype.$mount = function (

el?: string | Element,

hydrating?: boolean

): Component {

el = el && inBrowser ? query(el) : undefined

return mountComponent(this, el, hydrating)

}

$mount 方法支持传入 2 个参数,第一个是 el,它表示挂载的元素,可以是字符串,也可以是 DOM 对象,如果是字符串在浏览器环境下会调用 query 方法转换成 DOM 对象的。第二个参数是和服务端渲染相关,在浏览器环境下我们不需要传第二个参数。

$mount 方法实际上会去调用 mountComponent 方法,这个方法定义在 src/core/instance/lifecycle.js 文件中:

export function mountComponent (

vm: Component,

el: ?Element,

hydrating?: boolean

): Component {

vm.$el = el

if (!vm.$options.render) {

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 avail

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值