0
点赞
收藏
分享

微信扫一扫

vue3.0 watch的五种情况

素的盐 2022-04-04 阅读 59
javascript
	       //情况一:监视ref所定义的一个响应式数据
			 watch(sum,(newValue,oldValue)=>{
				console.log('sum变了',newValue,oldValue)
			},{immediate:true})


			//情况二:监视ref所定义的多个响应式数据
		    watch([sum,msg],(newValue,oldValue)=>{
				console.log('sum或msg变了',newValue,oldValue)
			},{immediate:true}) 

			
			//情况三:监视reactive所定义的一个响应式数据的全部属性
						1.注意:此处无法正确的获取oldValue
						2.注意:强制开启了深度监视(deep配置无效)
		    watch(person,(newValue,oldValue)=>{
				console.log('person变化了',newValue,oldValue)
			},{deep:false}) //此处的deep配置无效 


			//情况四:监视reactive所定义的一个响应式数据中的某个属性
			 watch(()=>person.name,(newValue,oldValue)=>{
				console.log('person的name变化了',newValue,oldValue)
			})

			//情况五:监视reactive所定义的一个响应式数据中的某些属性
			  watch([()=>person.name,()=>person.age],(newValue,oldValue)=>{
				console.log('person的name或age变化了',newValue,oldValue)
			})

			//特殊情况
			watch(()=>person.job,(newValue,oldValue)=>{
				console.log('person的job变化了',newValue,oldValue)
			},{deep:true}) //此处由于监视的是reactive素定义的对象中的某个属性,所以deep配置有效 */
举报

相关推荐

vue3.0

Vue3.0整理

vue3.0进阶

vue3.0 简介

vue3.0使用$ref

前端框架Vue3.0

vue3.0新特性

0 条评论