【发布时间】:2022-01-09 17:03:26
【问题描述】:
我想将购物车存储在 Vuex 商店中,我在我的 Index 组件中这样做:
storeCart: function (cookieValue) {
apiHelper.getRequest(
`/carts/${cookieValue}`,
(response) => {
this.$store.dispatch('storeCart', {
cart: response.data,
})
}
)
},
所以在mounted() 我正在检查是否有cookie,调用购物车,这样,即使我刷新页面,我也不使用购物车项目:
mounted() {
this.cartCookie = cookieHelper.getCookie(this.cartCookieName);
this.cartCookieValue = cookieHelper.getCookieValue(this.cartCookie);
if(this.cartCookie) {
this.storeCart(this.cartCookieValue);
}
},
但问题是,刷新页面后,我无法从购物车中添加、删除或更新产品。
我的行动:
export const storeCart = ({commit}, {cart}) => {
commit('STORE_CART', {cart});
}
还有我的突变:
export const STORE_CART = (state, {cart}) => {
state.cart = cart;
}
那么我存储数据的方式有什么问题?
【问题讨论】:
-
“但问题是,刷新页面后,我无法添加、删除或更新购物车中的产品”——不知道为什么会这样。请提供stackoverflow.com/help/mcve
-
“我无法从购物车中添加、删除或更新产品。”你有这些动作的功能吗?
-
而且在 storeCart 函数中,你可以立即使用突变,为什么要为此使用另一个动作?您可以将 storeCart 转移到正在运行的商店
标签: vue.js vuejs2 vue-component vuex vuex-modules