2017-05-04 121 views
2

添加新的计算值,我在VUEVUE计算:从创建生命周期挂钩

我试图推动与名称的计算数据初学者,这个名字来自vuex其中谈到创建实例后

如何将新的计算属性推到created()钩子中的实例?

下面是代码

computed: { 
      // 3 - i want to extract the object properties here computed 
      // that is easy bu using spread operator ... 
      // Problem : vue intialize computed before the created() hook 
      // so the spreed work befor passing the filling the object 
      ...this.mapGettersObj 
     }, 

     created(){ 
      // 1- i can access only this line after creating the object 
      this.stocks = this.$store.state 
      let arr=[] 
      for (let stock in this.stocks){ 
       arr.push(stock+'Getter') 
      } 
      // 2 - mapGetters returns an object 
      this.mapGettersObj=mapGetters(arr) 
     } 

如果我能创建将解决这个问题

+0

请包括你的工作代码,并表示你已经试过了没有奏效。 – PatrickSteele

+0

我已经插入的代码 –

回答

0

我不知道为什么你在做你做了什么之后创造新的计算值,但如果你想有计算被称为可以使用beforeCreate前的钩可变量:https://alligator.io/vuejs/component-lifecycle/

你也可以做一些在

+1

我认为这是一个好主意, ,但这并没有解决,因为该组件被创建后,才这样。$存储中创建,而不是有效的创建时间之前 所以调用计算的对象时在创建组件之前它没有看到这个。$ store.state –

+0

使用beforeCreate()钩子不能解决它吗? –

+0

谢谢 我认为这是一个好主意, ,但这并没有解决,因为该组件被创建后,才这样。$存储中创建并创建之前调用计算的对象时是无效的创建时间之前 所以组件它看不到这个$ store.state 我想知道在创建元素后将新的计算数据推送到计算对象,这可能吗? –

0

可以在beforeCreate挂钩做到这一点。

beforeCreate: function() { 
    this.$options.computed = { 
    demo2: function() { 
     return this.demo 
    } 
    } 
}