我一直得到同樣的錯誤,this.list。$ remove不是函數。我相信它與html標記有關,但不確定。任何人都可以將我指向正確的方向嗎?過去兩天我一直在掙扎。
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',
});
這是我的車款:
<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>
我相信這個問題是代碼中的其他位置。我把你的代碼和調整它很微調,看看我是否有同樣的錯誤,但它工作正常:https://jsfiddle.net/crswll/g1a1q3k3/ –
我擡頭看Vue Devtools,並注意到我得到的數據是對象一個對象而不是數組中的對象。所以我用虛擬數據替換爲一個數組的對象,它仍然給我同樣的錯誤。所以不是這樣。 我想知道如果Vue與其他JavaScript庫衝突? – Robbie
嗯。畢竟,我認爲問題與物體有關。當列表是一個數組時,。$ remove會起作用。當列表是一個對象時。$ remove給出錯誤,這不是一個函數。我查閱了文檔,但沒有提及此方法的對象。對於http://vuejs.org/guide/list.html#Object_v-for有對Object v的引用。任何幫助指出我在正確的方向將不勝感激。 – Robbie