【发布时间】:2019-07-10 18:33:54
【问题描述】:
我有这个问题:我正在尝试使用输入表单中的值修改对象的属性之一(示例中为<el-input>)。我们公司的堆栈使用 VueJs + TS + ElementUI。不幸的是,似乎对象的属性不能是反应性的并且处于只读模式。如何使对象属性具有反应性?
HTML/CSS
<el-input v-model="group.groupDescription" type="textarea"></el-input>
JS:
interface ViewGroupResult {
id: string;
groupDescription: string;
}
import Vue from 'vue';
import {Component, Watch} from 'vue-property-decorator';
@Component
apollo: {
group: {
query() {
group(groupId: $groupIdOrSlug) { ... }
}
},
variables() {
groupId: this.id
},
fetchPolicy: 'network-only',
loadingKey: 'groupLoading'
},
export default class ViewGroupPage extends Vue implements
group: ViewGroupResult | null = null;
当我尝试在上面的文本区域中键入内容时,该组对象的属性 group.groupDescription 的值永远不会更新,即使它与 v-model 指令绑定。
Error in event handler for "input": "TypeError: Cannot assign to read only property 'groupDescription' of object '#<Object>'"
我是否误解了 getter/setter 绑定到 VueJs 中数据属性的方式,或者这是 TypeScript 特有的问题?提前致谢!
【问题讨论】:
-
我实际上认为
Vue.set是可能的,但是否还有其他我不知道的解决方法? -
在尝试解决只读错误之前,我可能会调查为什么该属性被指定为只读。如果是故意的,那么您可能不应该尝试更改该属性。这可能会破坏应用程序中其他地方的某些东西。如果它只是一个过分热心的打字稿装饰,那就改变它。
标签: typescript vue.js element-ui