我不断收到同样的错误,即 this.list.$remove 不是函数。我相信它与 html 标记有关,但不确定。任何人都可以指出我正确的方向吗?在过去的 2 天里,我一直在努力。
Vue.component('cart-co', {
template: '#cart-template',
data: function() {
return {
list: []
}
},
ready: function() {
$.getJSON('cart/content', function(data) {
this.list = data;
}.bind(this));
},
methods: {
removeItem: function(item) {
console.log(item);
this.list.$remove(item);
}
}
});
new Vue({
el: 'body',
});
Run Code Online (Sandbox Code Playgroud)
这是我的购物车部分:
<cart-co></cart-co>
<template id="cart-template">
<div class="cart-content-wrapper">
<div class="cart-content" >
<ul v-if="list" class="scroller" style="height: 250px;">
<li v-for="item in list">
<a href="item.html"><img src="assets/temp/cart-img.jpg" alt="" width="37" height="34"></a>
<span class="cart-content-count">@{{ item.quantity }}</span>
<strong><a href="item.html">@{{ item.name }}</a></strong>
<em>@{{ item.price | currency }}</em>
<a href="#" class="del-goods" v-on:click="removeItem(item)"><i class="fa fa-times"></i></a>
</li>
</ul>
<ul v-else class="scroller" style="height: 250px;">
<li>Shopping cart is empty</li>
</ul>
<div class="text-right">
<a href="{{ route('cart.show-cart') }}" class="btn btn-default">View Cart</a>
<a href="checkout.html" class="btn btn-primary">Checkout</a>
</div>
</div>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
如果从调用返回的数据$.getJSON()是一个对象(购物车中的每个商品都是一个键值对),您可以按如下方式修改代码来处理删除商品。
假设数据看起来像这样:
{
"item1": { "name": "Name 1", "quantity": 1, "price": 10 },
"item2": { "name": "Name 2", "quantity": 1, "price": 10 },
"item3": { "name": "Name 3", "quantity": 1, "price": 10 }
};
Run Code Online (Sandbox Code Playgroud)
在删除链接中传递要删除的项目的密钥:
<a href="#" class="del-goods" v-on:click="removeItem($key)"><i class="fa fa-times"></i></a>
Run Code Online (Sandbox Code Playgroud)
将您的方法更改removeItem()为如下所示:
removeItem: function(key) {
Vue.delete(this.list, key);
}
Run Code Online (Sandbox Code Playgroud)
这使用Vue.delete方法删除属性(并确保视图对更改做出反应)。
| 归档时间: |
|
| 查看次数: |
13593 次 |
| 最近记录: |