在方法中直接将对象数组splice,watch监听的newVal和oldVal的值一样

场景:在方法中直接将对象数组splice,watch监听的newVal和oldVal的值一样

解决方法:在computed中

myQuestions2 (){
      return JSON.parse(JSON.stringify(window.localStorage.getItem("questions")))
    },

在watch中

 myQuestions2: {
      handler: function(newVal, oldVal) {
        if(oldVal&&newVal.length!==oldVal.length){
          this.myQuestions=JSON.parse(JSON.stringify(newVal))
        }
      },
      immediate: true,
      deep: true
    },

  这样就能监听,但是只是针对对象数组中没有包含特殊的形式,JSON.parse(JSON.stringify())不会导致其结果出现异常的情况。

posted @ 2021-03-15 16:24  yaqian96  阅读(301)  评论(0)    收藏  举报