vue3子传父

子组件

<template>
  <div>
    <button @click="btn">传参</button>
  </div>
</template>

<script setup>
import {ref} from 'vue';
const son = ref('son');
const age = ref(18);
const number = ref(99);
//定义了一个名为 emits 的对象,它使用了 defineEmits 函数,并传入了一个字符串数组,用于定义组件可以发射的事件名。
const emits = defineEmits(['handle']);
const btn = () => {
  // 调用了 emits 函数,并传入了 'handle' 和 son 两个参数,这表示组件会发射名为 'handle' 的事件,并传递一个值为 son 的参数。
  emits('handle', son, age, number);
};
</script>

父组件

<template>
  <div class="about">
    {{ sonData }}
    <Son @handle="handleClick"></Son>
  </div>
</template>

<script setup>
import Son from '@/components/son.vue';
import {ref} from 'vue';
const sonData = ref();
const handleClick = (...son) => {
  sonData.value = son;
};
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值