【发布时间】:2019-05-18 03:59:47
【问题描述】:
我想通过在 Nuxtjs 应用程序中使用 Vue.set() 来设置一个属性。
editPost(post) {
Vue.$set(post, 'edit', true)
}
得到错误:Vue 未定义
【问题讨论】:
-
import Vue from 'vue'在script的顶部。
我想通过在 Nuxtjs 应用程序中使用 Vue.set() 来设置一个属性。
editPost(post) {
Vue.$set(post, 'edit', true)
}
得到错误:Vue 未定义
【问题讨论】:
import Vue from 'vue' 在script 的顶部。
您可以只使用this 而不是Vue.。这引用了方法内的当前 vue 组件,因此它的工作方式相同:
editPost(post) {
this.$set(post, 'edit', true)
}
【讨论】:
有时您可能希望将多个属性分配给现有对象,例如使用 Object.assign() 或 _.extend()。但是,添加到对象的新属性不会触发更改。在这种情况下,使用原始对象和 mixin 对象的属性创建一个新对象:
// 代替Object.assign(this.someObject, { a: 1, b: 2 })
this.someObject = Object.assign({}, this.someObject, { a: 1, b: 2 })
【讨论】: