我在Vue.js中創建一個自定義組件。在我的組件中,我有一個列表,其中包含一個刪除按鈕。單擊一個按鈕時,它將刪除該行。如果單擊任何行,它將刪除最後一行,因爲索引爲什麼總是-1
? 這裏是我下面整個代碼代碼 https://plnkr.co/edit/hVQKk3Wl9DF3aNx0hs88?p=preview爲什麼index-of在vuejs中無法正常工作?
methods: {
deleteTodo:function (item) {
console.log(item)
var index = this.items.indexOf(item);
this.items.splice(index, 1);
}
}
var MyComponent = Vue.extend({
template:'#todo-template',
props:['items'],
computed: {
upperCase: function() {
return this.items.map(function (item) {
return {name: item.name.toUpperCase(),complete:item.complete};
})
}
},
methods: {
deleteTodo:function (item) {
console.log(item)
var index = this.items.indexOf(item);
this.items.splice(index, 1);
}
}
})
Vue.component('my-component', MyComponent)
var app = new Vue({
el: '#App',
data: {
message: '',
items: [{
name: "test1",
complete:true
}, {
name: "test2",
complete:true
}, {
name: "test3",
complete:true
}]
},
methods: {
addTodo: function() {
this.items.push({
name:this.message,
complete:true
});
this.message ='';
},
},
computed: {
totalCount:function() {
return this.items.length;
}
}
});