watch监听器

106 阅读1分钟
监听器
	watch:{}
		watch:{}
			watch:{
            isHot:{
                immediate:false,
                handler(newValue,oldValue){
                    console.log('isHot被修改了',newValue,oldValue)
                }
            }
        }
			 vm.$watch('isHot', {
        immediate: true,
        handler(newValue, oldValue) {
            console.log('isHot被修改了', newValue, oldValue)
        }})
		handler
			是数据变化是执行的一个方法  默认的 不能修改
		immediate:false
			立即执行
				默认值false
		watch默认不监视内部改变(一层)
		deep
			深度监视
				默认false
				可以监视对象内部值改变
		对象里面的key 要用引号套起来
			'obj.a’:{}
			obj['a']
		如果不需要immediate和deep可以简写
			 isHot(newValue,oldValue){
                console.log('asdasdasd')
            }
			newValue.a会报错是因为newValue是个变量 不能传参 只能用newValue['a']
			vm.$watch('isHot',function(newValue,oldValue){
                console.log('asdasdasd')
            })
		简写不能使用箭头函数
		可以开启异步任务