0
我有這樣的代碼:Vue公司刪除子組件
Vue.component('parent', {
template: `
<div>
<child v-for='(child, index) in children' :key='index' :childNumber="index+1" v-on:removeChild="removeChild" />
</div>
`,
data: function() {
return {
children: [{}, {}, {}]
}
},
methods: {
removeChild: function(index) {
this.children.splice(index, 1);
}
}
});
Vue.component('child', {
template: `
<div>
<input :value="'I am child number: '+childNumber"></input>
<button v-on:click="removeChild">Remove child {{childNumber}}</button>
</div>
`,
data: function() {
return {}
},
methods: {
removeChild: function() {
this.$emit('removeChild', this.childNumber);
}
},
props: ['childNumber']
});
const app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!',
}
});
此刻,當你點擊任何的「刪除」按鈕,將刪除最後一個孩子,無論你點擊了哪個按鈕。我如何更改我的代碼,以便刪除您認爲將要刪除的孩子,而不觸及其他孩子? (即點擊「刪除子2」將只留下孩子1和3的屏幕上。)
小提琴:https://jsfiddle.net/wgr3sxqr/6/
完美。謝謝。 –