【问题标题】:How to use another computed property inside mounted or created hooks?如何在挂载或创建的钩子中使用另一个计算属性?
【发布时间】:2020-09-29 12:24:48
【问题描述】:

这是我的代码?

computed: {
    posts() {
        return this.$store.getters['posts/postsByUser']
    },
    
    loggedIn() {
        return this.$store.getters['auth/check'];
    },
    user() {
        return this.$store.getters['auth/user']
    }
},
created() {
    this.$store.dispatch('posts/fetchPostsByUser', this.user.id)
}

如果您需要更多许可,请告诉我

【问题讨论】:

  • this.user.id 是你引用它的方式
  • 是的,这是一个拼写错误
  • 有什么问题?你的dispatch 不工作吗?你遇到了什么错误?
  • 调度时,this.user.id 未定义。这意味着它没有加载ye

标签: vue.js vuex


【解决方案1】:

使用这个在 Vue 组件实例上可用的计算属性(用户)

created() {
    this.$store.dispatch('posts/fetchPostsByUser', this.user.id)
}

【讨论】:

  • 也许你的计算属性在创建的钩子完成后就准备好了。这是一个竞赛条件。尝试在 this.$nextTick() 中添加创建的代码
  • 你能解释一下吗?
猜你喜欢
  • 2018-10-05
  • 2018-07-14
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-07
  • 2020-05-17
  • 1970-01-01
相关资源
最近更新 更多