Vue中的computed是在生命周期的哪個(gè)階段執(zhí)行的?

1.在new Vue()的時(shí)候,vue\src\core\instance\index.js里面的_init()初始化各個(gè)功能

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) //初始化各個(gè)功能
}

2.在_init()中有這樣的一個(gè)執(zhí)行順序:其中initState()是在beforeCreatecreated之間

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

3.在initState()做了這些事情:

if (opts.props) initProps(vm, opts.props)//初始化Props
if (opts.methods) initMethods(vm, opts.methods)//初始化methods
if (opts.data) {
  initData(vm)} else {
  observe(vm._data = {}, true /* asRootData */)}//初始化data
if (opts.computed) initComputed(vm, opts.computed)//初始化computed

4.所以propsmethods,data以及computed的初始化都是在beforeCreatedcreated之間完成的。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容