vue3 组建传值

Vue3组件传值

defineProps,和defineEmites

defineProps 等同与vue2 的props
 

defineProps({
xxx:{
	type:类型,
	default:xxx
}
})

defineEmites

const emit = defineEmites(["xxx"])
// 触发事件
const btn = () => {
	emit("xxx",需要传的值)
}

1、父传子

父组件

//通过动态绑定的方式
 <template>
 	<div>
		<button>父组件</button>
		<Child :sex="sex" @FatherMethod"></Child>
	</div>
<template>

<script setup lang="ts">
//引入子组件
import Child from './child.vue';
import { ref } from "vue";
const sex = ref('male');
const FatherMethod = () => {
	console.log('嗨嗨嗨,我是父组件');
};
</script>

子组件

<template>
  <div>
    <button @click="ChildMethod">子组件</button>
  </div>
</template>
<script setup lang="ts">
//子组件接收
const props = defineProps(["sex"])
const ChildSex = props.sex;
//子组件中自定义事件
const emits = defineEmits(["FatherMethod"]);
const ChildMethod = () => {
	emits("FatherMethod");
};
</script>

2、子传父

父组件

 <template>
 	<div>
		<button @click="FatherMethod">父组件</button>
		<Child ref="Childs">{{sex}}</Child>
	</div>
<template>

<script setup lang="ts">
//引入子组件
import Child from './child.vue';
import { ref } from "vue";
//声明子组件
const Childs: any = ref(null);
const FatherMethod = () => {
	//子组件.value.方法
	Childs.value.ChildMethod();
};
</script>

子组件

<template>
  <div>
    <button>子组件</button>
  </div>
</template>
<script setup lang="ts">
//子组件中定义事件第一种方法
const emits = defineEmits(["sex"]);
//子组件中定义事件第一种方法
function height() {
	//第一个参数是提交的变量名,后面是参数
	emit('height', 192);
};
//子组件的方法
const ChildMethod = () => {
	console.log('嗨害嗨,我是子组件');
};
//暴露出去
defineExpose({
	ChildMethod
})
</script>

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue2和Vue3的传值方式有所不同,具体如下: Vue2的传值方式: 1. 父组件向子组件传值:使用props进行传值。 2. 子组件向父组件传值:使用$emit触发父组件的自定义事件。 3. 非父子组件之间的传值:使用一个空的Vue实例作为中央事件总线,通过$emit和$on进行传值Vue3的传值方式: 1. 父组件向子组件传值:仍然使用props进行传值,但是可以使用v-bind缩写语法。 2. 子组件向父组件传值:使用emits选项声明子组件可以触发的事件,然后使用$emit触发事件。 3. 非父子组件之间的传值:使用provide和inject进行传值,provide可以在父组件中提供数据,inject可以在子组件中注入数据。 下面是一个Vue3组件通信的例子: ```vue <template> <div> <child :message="message" @update="handleUpdate"></child> <p>父组件收到子组件的消息:{{ message }}</p> </div> </template> <script> import { ref } from 'vue' import Child from './Child.vue' export default { components: { Child }, setup() { const message = ref('Hello, child!') const handleUpdate = (newMessage) => { message.value = newMessage } return { message, handleUpdate } } } </script> ``` ```vue <template> <div> <p>子组件收到父组件的消息:{{ message }}</p> <button @click="handleClick">向父组件发送消息</button> </div> </template> <script> import { defineEmits, defineProps } from 'vue' export default { props: { message: String }, emits: ['update'], setup(props, { emit }) { const handleClick = () => { emit('update', 'Hello, parent!') } return { handleClick } } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值