Vue将属性添加到数据根级别并使其具有反应性 [英] Vue add property to data root level and make it reactive
本文介绍了Vue将属性添加到数据根级别并使其具有反应性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下代码:
let myData = {
a: 1
}
const app = new Vue({
data: myData
});
正如Vue docs所说的,当我们尝试添加如下所示的属性时,新的属性不是被动的:
As Vue docs says when we try to add a property like below, the new property isn't reactive:
myData.b = 12;
console.log(app.b); // undefined
Vue文档建议使用 Vue.set
:
Vue docs suggests using Vue.set
:
Vue.set(myData, 'b', 12);
console.log(app.b); // still undefined
Vue文档说, Vue.set
为响应对象添加了一个属性!
Vue docs says Vue.set
adds a property to a reactive object!
但是我想在数据的根目录级别添加 b
.我不知道如何使 b
具有反应性.
But I want to add b
to the data's root level. I don't know how to make b
reactive.
推荐答案
查看全文