【发布时间】:2020-10-01 08:52:34
【问题描述】:
我是 Vuejs 的新手,但我来自 React,只是想在这里使用 Vuex。
我使用包含数组cart 的状态构建了我的商店。
const state = () => ({
cart: []
})
我想要一个函数 isInCart(product) 来返回产品是否在当前购物车中。
<span v-if="isInCart(product)">hello</span>
我应该使用商店的 getters 吗?
const getters = {
isInCart: (state) => (product) => state.cart.find(_product => _product.id === product.id) !== undefined,
}
然后在我的组件中像这样使用它?
computed: {
...mapGetters(['isInCart'])
},
但我有:Computed property "isInCart" was assigned to but it has no setter.。
编辑
我忘了在 getter 前面加上它的模块名称。
...mapGetters({ isInCart: 'shop/isInCart' }) // module name = shop
【问题讨论】: