[英]Update data property / object in vue.js
is there a way I can programmatically update the data
object / property in vue.js?有没有办法以编程方式更新 vue.js 中的data
对象/属性? For example, when my component loads, my data object is:例如,当我的组件加载时,我的数据对象是:
data: function () {
return {
cars: true,
}
}
And after an event is triggered, I want the data
object to look like:触发事件后,我希望data
对象看起来像:
data: function () {
return {
cars: true,
planes: true
}
}
I tried:我试过了:
<script>
module.exports = {
data: function () {
return {
cars: true
}
},
methods: {
click_me: function () {
this.set(this.planes, true);
}
},
props: []
}
</script>
But this gives me the error this.set is not a function
.但这给了我错误this.set is not a function
。 Can someone help?有人可以帮忙吗?
Thanks in advance!提前致谢!
Vue does not allow dynamically adding new root-level reactive properties to an already created instance. Vue 不允许动态地将新的根级响应属性添加到已创建的实例中。 However, it's possible to add reactive properties to a nested object, So you may create an object and add a new property like that:但是,可以将反应属性添加到嵌套对象,因此您可以创建一个对象并添加一个新属性,如下所示:
data: function () {
return {
someObject:{
cars: true,
}
}
and add the property with thevm.$set
method:并使用vm.$set
方法添加属性:
methods: {
click_me: function () {
this.$set(this.someObject, 'planes', true)
}
}
for vue 1.x
use Vue.set(this.someObject, 'planes', true)
对于vue 1.x
使用Vue.set(this.someObject, 'planes', true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.