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

定义全局的变量

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 字符串转换为对象)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。