2 回答

TA貢獻1802條經驗 獲得超10個贊
作為一種選擇:
var _results = [{name: 'toto', description: "titi" }];
var app = new Vue({
el: '#grid',
data: {
entries: _results
}
})
socket.on('get_entries', function(data){
console.log(_results);
console.log(data);
// Both logs show the same result (see below)
_results[0].description = data[0].description // This works! The description of the 1st item is updated
_results.splice(0, data.length, ...data); // This doesn't work
});

TA貢獻1895條經驗 獲得超3個贊
我相信有一種方法可以更新整個內容array,而無需做額外JavaScript的事情來觸發反應,但使用Vue必須提供的東西。
為此,您可能需要使用掛鉤,以便我們可以使用socket來更新。created()arrow functionthisentries
這樣我們就data可以直接觸發屬性的反應。
import io from 'socket.io-client';
var _results = [{name: 'toto', description: "titi" }];
var app = new Vue({
el: '#grid',
data: {
entries: _results,
socket: io()
},
created() {
this.socket.on('get_entries', (data) => {
this.entries = data;
});
}
})
這也適用于您的情況嗎?
添加回答
舉報