vue3+uni——watch监听props中的数据(组件参数接收与传递defineProps、defineEmits)

案例说明

A页面引用的子组件B

A页面

<template>
	<view>
	    //引用组件
		<serviceOrder @change="change" :list="list" :current="type"></serviceOrder>
	</view>
</template>

<script setup>
	import serviceOrder from '@/components/serviceOrder/serviceOrder.vue'
	const type = ref(0)
	// 切换tab
	function change(e) {
		type.value = e
	}
</script>

B子组件

<template>
	<view>
		<view class="orderTab">
			<block v-for="(item,index) in tabs" :key="index">
				<view class="tabItem" @click="change(index)" :class="index == current2?'active':''">
					{{item.name}}
				</view>
			</block>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		watch
	} from 'vue'
	// 接收值
	const props = defineProps({
		current: {
			type: Number,
			default: 0
		}
	})
	// 接收方法
	const emit = defineEmits(['change'])
	const tabs = ref([{
		name: '全部',
		type: 0
	}, {
		name: '待付款',
		type: 1
	}, {
		name: '待服务',
		type: 2
	}, {
		name: '已完成',
		type: 3
	}])
	const current2 = ref(0) //默认索引为0的样式
	watch(
		() => props.current,
		(newVal, oldVal) => {
			current2.value = newVal
		}
	)
	// 切换
	const change = (index) => {
		current2.value = index // 样式处理
		emit('change', current2.value)//传递方法
	}
</script>

<style lang="scss" scoped>
.orderTab {
	display: flex;
	justify-content: space-between;
	align-items: center;
	height: 88rpx;
	background: #FFFFFF;
	padding: 0 29rpx;

	.tabItem {
		font-size: 28rpx;
		color: #636363;
	}

	.active {
		position: relative;
	}

	.active:after {
		content: '';
		position: absolute;
		left: 50%;
		bottom: -10rpx;
		transform: translateX(-50%);
		width: 40rpx;
		height: 5rpx;
		background: #2CB6DB;
		border-radius: 50rpx;
	}
}
</style>

在这里插入图片描述

在此案例中,子组件中 用来通过判断来显示蓝色下划线样式的 变量为什么没有用current而是用的current2,你会发现组件接收的值不能在下面方法中二次处理使用,所以这个又定义了一个变量current2,改怎么写就怎么写样式判断。之后tab点击的事件暴露出去,在A页面里面使用。
当A页面current属性值被修改的时候,假如C页面中点击某个待服务跳转到A页面并传了参数2,这时候A页面接受该参数2,并将type的类型赋值为2,之后为了对应的效果显示,需要告诉B组件,目前应该是处于谁被选中的状态【下图】。所有就有了:current = "type"
在这里插入图片描述
子组件使用 defineProps接收传过来的参数2,watch一直监听这个变量,一旦这个变量有变化,那么执行操作,current2等于最新的2,这样“待服务”下面就有蓝条条了。

watch(
	() => props.current,
	(newVal, oldVal) => {
		current2.value = newVal
	}
)

问题

如果遇到了ref、watch is not defined等报错,
vue3的setup里面直接使用watch会报错,需要使用import引入之后才可使用。

import { ref,	watch	} from 'vue'
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3,可以使用`watch`函数来监听组件传给子组件的值。根据引用[1]的代码,子组件通过`defineExpose`暴露了自己的属性`inputVal`和方法`exposeFun`。在父组件,可以使用`watch`函数来监听组件的属性变化。例如,如果要监听组件的`inputVal`属性,可以这样写: ```javascript import { watch } from 'vue'; watch(() => SonVue.inputVal, (newVal, oldVal) => { console.log('子组件的inputVal发生变化', newVal, oldVal); }); ``` 这样,当子组件的`inputVal`属性发生变化时,回调函数就会被触发,并打印出新值和旧值。需要注意的是,`watch`函数的第一个参数是一个函数,返回要监听的值,而不是直接传入要监听的值本身。所以在这里,我们使用了箭头函数来返回`SonVue.inputVal`。 另外,根据引用[3]的代码,`watch`函数还可以监听多个值或对象的属性。如果要监听多个值,可以将这些值放在一个数组传入`watch`函数。如果要监听对象的属性,可以将一个函数返回这个属性的值。例如,如果要监听组件的`inputVal`和父组件的`msg`,可以这样写: ```javascript watch([() => SonVue.inputVal, () => msg.value], ([newInputVal, newMsg], [oldInputVal, oldMsg]) => { console.log('子组件的inputVal和父组件的msg任意一个发生变化都触发', newInputVal, oldInputVal, newMsg, oldMsg); }); ``` 这样,当子组件的`inputVal`或父组件的`msg`发生变化时,回调函数就会被触发,并打印出新值和旧值。 总结起来,Vue3可以使用`watch`函数来监听组件传给子组件的值,可以监听单个值、多个值或对象的属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值