• 处理组件配置项;初始化根组件时进行了选项合并操作,将全局配置合并到根组件的局部配置上;初始化每个子组件时做了一些性能优化,将组件配置对象上的一些深层次属性放到 vm.$options 选项中,以提高代码的执行效率;
    • 初始化组件实例的关系属性,比如 p a r e n t 、 parent、parent、children、r o o t 、 root、root、refs 等
    • 处理自定义事件
    • 调用 beforeCreate 钩子函数
    • 初始化组件的 inject 配置项,得到 ret[key] = val 形式的配置对象,然后对该配置对象进行响应式处理,并代理每个 key 到 vm 实例上
    • 数据响应式,处理 props、methods、data、computed、watch 等选项
    • 解析组件配置项上的 provide 对象,将其挂载到 vm._provided 属性上
    • 调用 created 钩子函数
    • 如果发现配置项上有 el 选项,则自动调用 $mount 方法,也就是说有了 el 选项,就不需要再手动调用 $mount 方法,反之,没提供 el 选项则必须调用 $mount
    • 接下来则进入挂载阶段

      1. // core/instance/init.js
      2. export function initMixin (Vue: Class<Component>) {
      3. Vue.prototype._init = function (options?: Object) {
      4. const vm: Component = this
      5. vm._uid = uid++
      6. // 如果是Vue的实例,则不需要被observe
      7. vm._isVue = true
      8. if (options && options._isComponent) {
      9. // optimize internal component instantiation
      10. // since dynamic options merging is pretty slow, and none of the
      11. // internal component options needs special treatment.
      12. initInternalComponent(vm, options)
      13. } else {
      14. vm.$options = mergeOptions(
      15. resolveConstructorOptions(vm.constructor),
      16. options || {},
      17. vm
      18. )
      19. }
      20. if (process.env.NODE_ENV !== 'production') {
      21. initProxy(vm)
      22. } else {
      23. vm._renderProxy = vm
      24. }
      25. vm._self = vm
      26. initLifecycle(vm)
      27. initEvents(vm)
      28. callHook(vm, 'beforeCreate')
      29. initInjections(vm) // resolve injections before data/props
      30. initState(vm)
      31. initProvide(vm) // resolve provide after data/props
      32. callHook(vm, 'created')
      33. if (vm.$options.el) {
      34. vm.$mount(vm.$options.el)
      35. }
      36. }
      37. }