关于watch中deep: true的用法

最近在使用deep:true时发现,当监听对象中的某个字段,也就是具体到某一个字段时,就不需要使用deep:true了。deep:true主要是对对象进行深度监听。以我下面的举例:
当监听’searchData.time’时不需要使用深度监听,但若改成’searchData’,添加deep:true监听这个对象,只要这个对象中有值变化,watch中的代码就会运行。

在子组件中:

<template>
	<div>
		...
	</div>
</template>
<script>
import { dateFormat } from '@/utils/formatDate'
import { calDateRange } from '@/utils/util'
export default {
	name: 'Search',
	data() {
		return {
			// 查询条件
			searchData: {
				time: 'week',
				sex: '女',
				age: 18
			},
		}
	},
	watch: {
		// 获取开始时间和结束时间
		'searchData.time': {
			handler(selectDay) {
				console.log(selectDay)
				this.search()
			},
			// deep: true,  如果改成监听searchData,然后把这行放开,就可以监听到searchData中任一值
		},
	},
	methods: {
		search() {
			const params = {
				sex: this.searchData.sex,
				age:  this.searchData.age
			}
			this.$emit('search', params)
		},
	},
}
</script>
<style lang="less" scoped>
</style>

在父组件中:

<template>
	<div class="content">
		<div class="homeBody">
			<Search @search="queryListParams"></Search>
		</div>
	</div>
</template>
<script >
import Search from './components/Search.vue'

export default {
	name: 'ReportList',
	components: {
		Search,
	},
	data() {
		return {
			queryParams: {},
		}
	},
	methods: {
		queryListParams(params) {
			this.queryParams = { ...params }
		},
	},
}
</script>
<style lang="less" scoped>
</style>

但我遇到的一个问题是:我在data中searchData: { time: ‘week’, sex: ‘女’, age: 1 },在template标签中searchData还双向绑定了另一个searchData.school,我对searchData.time进行监听,但不小心写上了deep:true,导致school和time的改变都会影响到监听

不知道理解的对不对?欢迎指正~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值