vue Object.assign 导致数据回填,输入框不可以输入,也不可以删除值。

1、业务场景,在页面中,监听输入框值改变,就将值存入本地缓存。下次进入页面的时候,提醒用户还有没保存的数据,是否立即编辑。

2、代码如下:

 watch: {
      form: {
        handler(newVal) {
          this.saveForm();
        },
        deep: true
      },
      otherForm: {
        handler(newVal) {
          this.saveForm();
        },
        deep: true
      }
}
 methods: {
    saveForm() {
        let self = this;
        let form = Object.assign(self.form, self.otherForm);
        localStorage.setItem("form", JSON.stringify(form));
    },
},

3、解决:

      saveForm() {
        let self = this;
        let formbrefore = JSON.parse(JSON.stringify(self.form))
        let form = Object.assign(formbrefore, self.otherForm);
        localStorage.setItem("form", JSON.stringify(form));
      },

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章