我需要观看一个 vue-resource HTTP 获取请求。如果找到新数据,我希望推送到v-for循环,如果发现数据被删除,我想删除它而不重新渲染整个v-for数据。
JS
new Vue({
el: '#chats',
http: {
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
},
data: {
chats: []
},
watch: {
/*
* I need to watch the 'chats' data
* if found new data, I want that push it
* if found data deleted I want to remove it
*/
},
methods: {
indexChats: function() {
/* vue-resource
* I need to watch this http get data for changes
*/
this.$http.get('/api/chats').then((response) => {
this.$set('chats', response.json());
}, (response) => {
console.log('could not fetch chats from api');
});
}
},
ready: function() {
this.indexChats();
}
});
HTML
我想在不完全重新渲染整个v-for循环的情况下显示聊天。只需更新观察者发现的更改。
<ul id="chats">
<li v-for="chat in chats">{{ chat.message }}</li>
</ul>