我尝试使用原型(prototype)将属性设置为数据中定义的空对象,但出现该对象未定义的错误,我在使用“原型(prototype)”时看到错误,我必须做什么?
这是针对 vue@2.6.10 的,还使用了 vue-router@3.1.3 和 vuex@3.1.1。 下面的代码是在另一个组件中导入的组件的一部分。
<template>
<input class="input" v-model="RealName" placeholder="Your Name"/>
...
</template>
<script>
export default {
name: "Person",
data() {
return {
Email: null,
RealName: null,
Ncode: null,
City: null,
Education: null,
Phone: null,
static: {}
}
},
watch: {
RealName: function(changed, lastOne){
this.static.prototype.firstRealName = this.static.firstRealName | lastOne // -- Ttrouble --
console.log(this.static.firstRealName + ': ' + lastOne +' => ' + changed)
}
}
};
</script>
当我编辑输入时,我在控制台上收到此错误: “TypeError:无法设置未定义的属性“firstRealName”...”
最佳答案
而不是
this.static.prototype.firstRealName = this.static.firstRealName | lastOne
你可以使用
this.$set(this.static, "firstRealName", this.static.firstRealName | lastOne);
文档here
关于javascript - 如何为vue js数据中定义的对象设置属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57762830/