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 <html> or <body> - mount to normal elements instead.` ) returnthis }
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 ) } } } elseif (template.nodeType) { template = template.innerHTML } else { if (process.env.NODE_ENV !== 'production') { warn('invalid template option:' + template, this) } returnthis } } elseif (el) { template = getOuterHTML(el) } if (template) { /* istanbul ignore if */ if (process.env.NODE_ENV !== 'production' && config.performance && mark) { mark('compile') }
exportfunctionquery (el: string | Element): Element{ if (typeof el === 'string') { const selected = document.querySelector(el) if (!selected) { process.env.NODE_ENV !== 'production' && warn( 'Cannot find element: ' + el ) returndocument.createElement('div') } return selected } else { return el } }
query方法返回 DOM 结构,若不存在,返回<div></div>,具体来说
1
<divid="app">{{ text }}</div>
1 2 3 4 5 6 7 8 9 10 11
var app = new Vue({ data: { text: 'Hello World' } }).$mount('#app'); // 或者 var app = new Vue({ data: { text: 'Hello World' } }).$mount(document.getElementById('app'));
在 console 中输入app.$el
1 2 3
app.$el
// <div id="app">Hello World</div>
$mount方法传入的参数el有一定的限制
当它是一个错误的字符串时
1 2 3 4 5 6 7
var app = new Vue({ data: { text: 'Hello World' } }).$mount('asd');
// [Vue warn]: Cannot find element: asd
在 console 中,
1 2 3
app.$el
// <div></div>
改写后的Vue.prototype.$mount规定了el不能挂载到body或者html上
1 2 3 4 5 6 7 8
// code ... 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.` ) returnthis } // code ...
举例来说
1 2 3 4 5 6 7
var app = new Vue({ data: { text: 'Hello World' } }).$mount(document.body);
// [Vue warn]: Do not mount Vue to <html> or <body> - mount to normal elements instead.
exportfunctionmountComponent ( vm: Component, el: ?Element, hydrating?: boolean ): Component{ vm.$el = el if (...) { // code ... } else { warn( 'Failed to mount component: template or render function not defined.', vm ) } // code ... }
举例来看
1 2 3 4 5 6 7
var app = new Vue({ data: { text: 'Hello World' } }).$mount(document.getElementById('asd'));
// [Vue warn]: Failed to mount component: template or render function not defined.
exportfunctionmountComponent ( 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 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 ) } } } callHook(vm, 'beforeMount')
let updateComponent /* 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}`
// 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 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 }
该方法首先缓存了当前的 DOM,然后判断是否有render函数,如果没有则报错;
紧接着设置Wathcer,Watcher在这里起到两个作用,一个是初始化的时候会执行回调函数,另一个是当 vm 实例中的监测的数据发生变化的时候执行回调函数,这块儿在之后介绍