03_new Vue都发生了什么

当我们new Vue的时候会进入Vue方法

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)
}


//把_init挂载到Vue原型中去的地方
initMixin(Vue)
stateMixin(Vue)
eventsMixin(Vue)
lifecycleMixin(Vue)
renderMixin(Vue)

那么这个_init是什么时候定义的,在执行initMixin方法的时候定义的

init为我们做了什么
//
合并选项 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 ) } // 初始化生命周期 initLifecycle(vm) // 初始化事件 initEvents(vm) // 初始化render函数 initRender(vm) // 执行beforeCreate钩子 callHook(vm, 'beforeCreate') // 初始化initInject initInjections(vm) // resolve injections before // *(重要)初始化状态method props data等 initState(vm) // 初始化provide initProvide(vm) // resolve provide after data/props // 调用created钩子 callHook(vm, 'created')

 

posted @ 2022-04-12 00:37  Mr-Hou88888  阅读(65)  评论(0编辑  收藏  举报