首页 > 其他分享 >vue2 data 一定要是函数吗?

vue2 data 一定要是函数吗?

时间:2023-02-11 19:33:37浏览次数:59  
标签:function use 函数 object vm vue2 data

答:不一定,可以是对象,vue文档中表述有误导性而已,他自己的例子里也有对象的形式


function initData(vm){

  let data = vm.$options.data

  data = vm._data = typeof data === ‘function’ ? getData(data, vm) : data || {}
   /*

     Because here,data is a reference from vm.$options.data, 
     if data is an object, 
     when there are many instances of this Component,
     they all use the same `data`


      if data is a function, Vue will use method getData( a wrapper to executing data function, adds some error handling)

      and return a new object, this object just belongs to current vm you are initializing

    */

   ……


  // observing data
  observe(data, true)

}

内容来自:https://stackoverflow.com/a/61184547/17160403

标签:function,use,函数,object,vm,vue2,data
From: https://www.cnblogs.com/dou-fu-gan/p/17112403.html

相关文章