2020-09-25 VUE 保存旧值和获取新值

2020-09-25  本文已影响0人  我的的昵称已被使用换一个吧

定义全局的变量

var beforeData = "null";

错误写法

getInfo(this.queryFormData)
        .then((res) => {
          this.loading = false;
          this.tableData = res.data.list || [];
          
this.beforeData = res.data;
          
          console.log("======================================="+this.beforeData.accountBank);
          this.queryFormData = res.data;
        })

正确写法

getInfo(this.queryFormData)
        .then((res) => {
          this.loading = false;
          this.tableData = res.data.list || [];
          
this.beforeData = JSON.parse(JSON.stringify(res.data));
          
          console.log("======================================="+this.beforeData.accountBank);
          this.queryFormData = res.data;
        })

原因:
this.beforeData相当于一个指针,赋值之后实际上是将其指向了res.data,而后面的 this.queryFormData(与页面组件进行了双向绑定) 又指向了res.data,所以改变页面组件值的时候this.beforeData也会跟着一起变,所以此时应该重新给beforeData开辟一段新的空间用于存放旧值
(JSON.parse() 方法用于将一个 JSON 字符串转换为对象)

上一篇 下一篇

猜你喜欢

热点阅读