VUE3(七)侦听器

其它方式

只监听内部的一个属性:

            watch:{
                "info.name": function (newValue, oldValue) {
                    console.log(newValue, oldValue);
                }
            }

通过生命周期来绑定侦听器,在options中的created中:

            created(){
                const unwatch=this.$watch("info",function (newValue, oldValue) {
                    console.log(newValue, oldValue);
                },{
                    deep: true,
                    immediate: true
                });
                // 取消侦听
                // unwatch();
            }

该方法会返回一个unwatch,可以调用之以取消侦听。

发表评论