對於我的應用程序,我使用了兩個Vue組件。一個呈現「天」的列表,一個呈現每個「日」的「位置」列表。因此,例如「第一天」可以具有「柏林」,「倫敦」,「紐約」的位置。vue2 v-for嵌套組件道具在父元素被刪除後不會更新
所有內容都呈現正常,但從視圖未被修正的天數列表中刪除「第1天」後。這是發生了什麼:
- 被刪除被替換一天的標題 - >正確
- 被刪除未替換一天的內容 - >不正確
Vue.component('day-list', {
props: ['days'],
template: '<div><div v-for="(day, index) in dayItems">{{ day.name }} <a href="#" @click.prevent="remove(index)">Remove day</a><location-list :locations="day.locations"></location-list><br/></div></div>',
data: function() {
return {
dayItems: this.days
}
},
methods: {
remove(index) {
this.dayItems.splice(index, 1);
}
}
});
Vue.component('location-list', {
props: ['locations', 'services'],
template: '<div><div v-for="(location, index) in locationItems">{{ location.name }} <a href="#" @click.prevent="remove(index)"</div></div>',
data: function() {
return {
locationItems: this.locations
}
},
methods: {
remove(index) {
this.locationItems.splice(index, 1);
}
}
});
const app = window.app = new Vue({
el: '#app',
data: function() {
\t return {
\t days: [
{
name: 'Day 1',
locations: [
{name: 'Berlin'},
{name: 'London'},
{name: 'New York'}
]
},
{
name: 'Day 2',
locations: [
{name: 'Moscow'},
{name: 'Seul'},
{name: 'Paris'}
]
}
]
}
},
methods: {}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.3/vue.js"></script>
<div id="app">
<day-list :days="days"></day-list>
</div>
這是一個工作jsFiddle以供參考:https://jsfiddle.net/2gab0k3y/ – Mani