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;
});
}
})
这也适用于您的情况吗?
添加回答
举报
