new Vue()做了什么?


Vue.prototype._init = function (options?: Object) {
    // a flag to avoid this being observed
    vm._isVue = true
    // merge options
    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
      )
    }
    /* istanbul ignore else */
    if (process.env.NODE_ENV !== 'production') {
      initProxy(vm)
    } else {
      vm._renderProxy = vm
    }
    // expose real self
    vm._self = vm
    initLifecycle(vm)
    initEvents(vm)
    initRender(vm)
    callHook(vm, 'beforeCreate')
    initInjections(vm) // resolve injections before data/props
    initState(vm)
    initProvide(vm) // resolve provide after data/props
    callHook(vm, 'created')
    if (vm.$options.el) {
      vm.$mount(vm.$options.el)
    }
  }

  1. initProxy,作用域代理,攔截組件內訪問其它組件的數據。
  2. initLifecycle建立父子組件關系,在當前實例上添加一些屬性和生命周期標識。如: $children $refs_isMounted 等。
  3. initEvents 用來存放除 @hook:生命周期鈎子名稱="綁定的函數"事件的對象。如: $on $emit 等。
  4. initRender 用於初始化 $slots $attrs $listeners
  5. initInjections初始化 inject ,一般用於更深層次的組件通信,相當於加強版子組件的 props 。用於組件庫開發較多。
  6. initState 是很多選項初始化的匯總,包括: props 、methods 、data 、computedwatch 等。
  7. initProvide 初始化 provide
  8. vm.$mount掛載實例。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM