【问题标题】:Vue.js2.0 - vue reactivity confusionVue.js2.0 - vue 反应性混乱
【发布时间】:2018-04-30 08:09:24
【问题描述】:

在我的项目中,我有一个要显示的购物清单数组。当组件被挂载时,存储被填充(它只包含一个用于记录客户的数组,从 API 数据库服务器获取......没有任何问题)

在显示时,我收到以下消息:

vue.esm.js?efeb:571 [Vue warn]: Property or method "shoppinglists" is not defined on 
the instance but referenced during render. Make sure that this property is reactive, 
either in the data option, or for class-based components, by initializing the 
property. 
See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

shoppinglists 属性定义为计算...

computed: {
  ...mapGetters([ { shoppinglists: 'getShoppingLists' } ])
},

并且商店包含购物清单数组

状态

{
 "shoppinglists":
 [{"title":"Groceries","items":[{"text":"Bananas","checked":true},
 {"text":"Apples","checked":false}],"id":1,"userId":1}],
 "isAuthenticated":true,
 "currentUserId":1
 }

如果我在数据中插入一个道具声明:

   data: function () {
  return {
    shoppinglists: []
  }
},

警告消失,但仍然没有显示列表..

可能有什么问题? 感谢反馈

不完全重复的问题,但离this one不远

【问题讨论】:

  • 你声明getShoppingLists getter了吗?如果是的话,你能发布它的声明吗?

标签: vue.js vuex


【解决方案1】:

看起来您已经混合了 mapGetters() 的两个不同选项。 你可以这样写:

import { mapGetters } from 'vuex'

export default {
  // ...
  computed: {
    // mix the getters into computed with object spread operator
    ...mapGetters([
      'doneTodosCount',
      'anotherGetter',
      // ...
    ])
  }
}

将this.doneTodosCount 映射到this.$store.doneTodosCount 等等。 或者你可以这样做,这可能是你想要的:

...mapGetters({
  // map `this.doneCount` to `store.getters.doneTodosCount`
  doneCount: 'doneTodosCount'
})

对于你的例子,这变成:

computed: {
  ...mapGetters({ shoppinglists: 'getShoppingLists' })
},

更多文档和示例来源在this article底部。

【讨论】:

  • 感谢@awnton,但是我写的内容有什么区别:computed: { ...mapGetters([ { shoppinglists: 'getShoppingLists' } ]) } 和你提到的(第二个选项): ...mapGetters({ // 将this.doneCount 映射到store.getters.doneTodosCount doneCount: 'doneTodosCount' })
  • 您将数组而不是对象传递给 mapGetters()。即您不小心将{ shoppinglists: 'getShoppingLists' } 包裹在[ ] 中。所以我更新你的例子的唯一区别是我删除了括号。
  • 哇……我累了……或者我需要换眼镜……你说得对!更好的两个括号!
猜你喜欢
  • 2017-07-25
  • 1970-01-01
  • 2023-02-19
  • 2017-04-24
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 1970-01-01
  • 2019-02-05
相关资源
最近更新 更多