【发布时间】:2021-03-25 19:52:46
【问题描述】:
我正在尝试从我的购物车中永久删除产品项目,但我创建的功能仅从 DOM 中删除数据,并且在浏览器重新刷新时重新出现。这是我到目前为止所做的。
cart.liquid 文件中带有 JS 点击功能的 HTML
<a @click="removeFromCart(item)" class="product-remove">remove</a>
我的 CartForm.js 文件中的 removeFromCart 函数
removeFromCart(item){
let productInfo = this.cart.items.reduce(
(accumulator, target) => ({ ...accumulator, [target.variant_id]: target.quantity}),
{});
const myJSON = JSON.stringify(productInfo);
axios.post('/cart/update.js', { updates: productInfo })
.then((res) => {
this.cart.items.splice(this.cart.items.indexOf(productInfo), 1);
})
.catch((error) => {
new Noty({
type: 'error',
timeout: 3000,
layout: 'topRight',
text: 'Cart not updated'
}).show();
})
},
任何建议都会很棒。谢谢
【问题讨论】:
标签: javascript api vue.js axios shopify