You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import { initMixin } from './init'
import { stateMixin } from './state'
import { renderMixin } from './render'
import { eventsMixin } from './events'
import { lifecycleMixin } from './lifecycle'
import { warn } from '../util/index'
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)
}
initMixin(Vue)
stateMixin(Vue)
eventsMixin(Vue)
lifecycleMixin(Vue)
renderMixin(Vue)
export default Vue
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
}
下面这段代码,vue内部做了什么操作?我去源码里面找找看
入口
vue 的入口文件在
src/core/instance/index.js
, 里面一进来就执行了很多初始化的操作。进入
initMixin
方法看看,这个方法内部只做了一件事,定义Vue.prototype._init
, 这个_init
方法又做了什么呢?解析
进入
src/platforms/web/entry-runtime-with-compiler.js
文件,看看$mount
方法是怎么处理模板的。el
是否为body或者html根节点,是的话,提示错误。src/compiler/index.js
对模板进行AST解析和静态优化,并重建render方法runtime/index.js
中的$mount
方法。而
$mount
方法调用src/core/instance/lifecycle.js
中的mountComponent
方法和VUE1的区别
在vue1.0种,模板的解析是通过
createDocumentFragment
对dom进行代理实现的,到了2.0时代,考虑到服务端渲染,采用了jquery作者开发的html-parse
库进行字符串模板解析。The text was updated successfully, but these errors were encountered: