如果我使用Vue 2中的道具傳遞一個對象數組,並且在這個數組上使用v-for指令,那麼view不會更新if其中一個數組元素被刪除。在Vue 2傳遞道具之後v-for不會在物品被刪除後得到更新
這似乎只有在V型的元素被聲明爲數據的工作,但我的組件需要接收道具......
在下面的例子中,你可以看到,services
數組中的元素的確已刪除,但v-for未被觸發。
我敢肯定,我在這裏做一些錯誤......
Vue.component('location-service-list', {
props: ['services'],
template: '<div>{{ services }}<div v-for="(service, index) in services">{{ service.id }} - {{ service.name }} <a @click.prevent="remove(index)">remove</a></div></div>',
methods: {
remove(index) {
this.services.splice(index, 1);
console.log(this.services);
},
}
});
const app = window.app = new Vue({
el: '#admin-app'
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.8/vue.js"></script>
<div id="admin-app">
<location-service-list :services='[{"id":1,"name":"Test 1"},{"id":2,"name":"Test 2"}]'></location-service-list>
</div>