Vue2x-Vue实例的挂载
runtime-only & runtime-with-compiler在用vue-cli构建应用时,一般会让我们做如下选择: Runtime + Compiler:recommended for most usersRuntime-only: about 6KB lighter min+gzip, but templates...其原因是$mount的实现方式在不同平台构建时具有差异性。 compiler的版本和runtime-only版本的差异就在于: runtime-only版本是只包含Vue.js运行时的代码,体积更轻量,通常需要借助vue-loader将.vue文件编译为.js,而compiler版本会在执行的过程中直接预编译。$mountVue.prototype.$mount = function ( el?: string | Element, hydrating?: boolean): Component { el = el && inBrowser ? query(el) : undefined return mountComponent(this, el, hydrating)}$mount方法接收两个参数,一是挂载对象,第二个跟服务器渲染相关,浏览器环境不需要传。其内部实际调用了/src/core/instance/lifecycle里的mountComponent方法: export function mountComponent ( vm: Component, el: ?Element, hydrating?: boolean): Component { vm.$el = el // ... callHook(vm, 'beforeMount') let updateComponent /* istanbul ignore if */ if (process.env.NODE_ENV !== 'production' && config.performance && mark) { // ... } else { updateComponent = () => { vm._update(vm._render(), hydrating) } } new Watcher(vm, updateComponent, noop, { before () { if (vm._isMounted && !vm._isDestroyed) { callHook(vm, 'beforeUpdate') } } }, true /* isRenderWatcher */) hydrating = false if (vm.$vnode == null) { vm._isMounted = true callHook(vm, 'mounted') } return vm}为了看着简洁,此处的源码删除了一些判断逻辑。 ...