【问题标题】:How can I refresh computed property如何刷新计算属性
【发布时间】:2019-12-27 02:55:09
【问题描述】:

我想对后续问题使用重新计算或其他方式。 当我保存从someData 呈现的项目时,更新的数据会发送到服务器。 我想刷新someData,以便我们可以从服务器看到更新的someData$recomputed('someData') 不工作。

我认为我应该使用Promise,但不知道如何以及在何处实现它。 如果有人在这个问题上帮助我,我将不胜感激。感谢您的帮助。

组件:

computed : someData : function() { 
  return this.getData()// this is getters in the store 
}
mounted() {
  this.pullData() //this is actions for fetching data in the store, 
  this action commits fetched data to the state for getters

计算属性someData 在模板中使用。 我要编辑并保存someData

商店

const actions = {
  async pullData(commit){
  await ...// fetch data from apis and commit 
  }
}

const getters = {
  getData : state.data
}

还实现了突变。

这不会渲染someData,我的意思是someData 在渲染后是空的 组件。

data : { someData : [] },
mounted (){
  this.pullData();
  this.someData = this.getData()
}

【问题讨论】:

    标签: api vue.js vuex computed-properties


    【解决方案1】:

    pullData 是一个async 操作,所以它已经返回了一个Promise。在从 getData 检索结果之前,您必须 await 对其结果:

    import { mapActions, mapGetters } from 'vuex'
    
    export default {
      async mounted () {
        await this.pullData()
        this.someData = this.getData
      },
      computed: {
        ...mapGetters(['getData'])
      },
      methods: {
        ...mapActions(['pullData'])
      }
    }
    

    【讨论】:

    • @FeiXiao 没问题 :)
    猜你喜欢
    • 2014-07-04
    • 2018-11-29
    • 2021-01-23
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 2019-06-18
    • 2015-01-28
    • 2019-04-18
    相关资源
    最近更新 更多