美文网首页
vue内部不同变量类型的watch

vue内部不同变量类型的watch

作者: 来了啊小老弟 | 来源:发表于2019-01-25 22:56 被阅读0次

1.普通变量(number,string...)

watch:{

  data:function(){        //注意不用箭头函数,理由是箭头函数绑定了父级作用域的上下文,所以 this 将不会按照期望指向 Vue 实例

        console.log("改变");

    }

},

2.数组

数组改变时建议用原生操作数据的方式进行改变,比如push之类。

return array=[1,2,3];

array2:{ handler(newValue, oldValue) { console.log(newValue) }, deep: true },



3.对象

return obj1 = {

    label:1,

    value:2

}

obj1.label=2;

watch:{

    obj1:{

        handler(newValue, oldValue) {

            console.log(newValue)

        },

        deep:true    //深拷贝

    }

}

//如果要检测某一个特定属性的改变,可以用computed

computed: { value() { return this.obj1.value } },

watch:{  

value(newValue, oldValue) { console.log(newValue,oldValue) },

相关文章

网友评论

      本文标题:vue内部不同变量类型的watch

      本文链接:https://www.haomeiwen.com/subject/nnivjqtx.html