javascript - 如何为vue js数据中定义的对象设置属性?

标签 javascript vue.js prototype

我尝试使用原型(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/

相关文章:

vue.js - 通过 Vue.js 中的数据禁用/启用按钮?

javascript - 单击按钮后停止页面回发

vue.js - 未知的自定义元素 : <v-app> - did you register the component correctly? 用于递归组件

javascript - Express 中间件中的异步等待不起作用

javascript - 如何使用CSS同时滑动两个元素?

javascript - JS 几个函数通过原型(prototype)

javascript - 使用 constructor.prototype 遍历原型(prototype)链

javascript - 为什么不在字符串连接操作中调用 Number.toString()?

javascript - Polymer.js 输入范围的默认参数

javascript - 在 JavaScript 中获取整个页面宽度的简单跨浏览器方法,而不仅仅是视口(viewport)宽度?