超详细vue3选项式父子组件传值

一、问题背景

最近遇到了一个情景:

子组件干完事情,需要对父组件的变量进行更新,因为父组件将该变量传递给子组件,但是不会双向绑定,这时候我们就需要传值或者触发回调去解决这个问题

我们将分为两个部分

1.父组件传值给子组件

2.子组件传值给父组件

来进行讲解

二、父组件传值给子组件

注意,本文中间部分只给部分代码,完整代码在文末!!!

2.1父组件传值给子组件示例

<ChildrenConponent @callBack="callBackFun" :datas="parentValue"></ChildrenConponent>

我们在父组件引入该子组件,然后随便定义一个要传入的变量,使用冒号:打头,后面紧跟需要传递的实际值变量

2.2子组件接收父组件传值

const props = defineProps( ["datas"]);
values.value = props.datas;

只需要引入defineProps解析传值列表然后取出对应属性即可

三、子组件传值给父组件

3.1子组件传值给父组件示例

首先看父组件

<ChildrenConponent @callBack="callBackFun" :datas="parentValue"></ChildrenConponent>

在使用冒号传入给子组件的值的同时,使用@callBack告诉子组件他可以调用一个名为callBack的回调,该回调后面紧跟一个声明在父组件的触发方法callBackFun

3.2父组件截取子组件调用回调示例

然后是子组件

const emit = defineEmits(['callBack']);
emit('callBack', childValue.value);

首先导入defineEmits获取回调列表获取一个函数

然后使用回调函数传值个父组件即可(注意保持参数列表一致)

四、全部代码

4.1父组件

<template>
    <div>
        你好!!!,我是父组件,我拿到了值:({{ values }})
    </div>
    <ChildrenConponent @callBack="callBackFun" :datas="parentValue"></ChildrenConponent>
</template>

<script setup>
import { ref } from 'vue'
import { ChildrenConponent } from './index'

const parentValue = ref("给儿子的值")
const values = ref("")
const callBackFun = (e) => {
    console.log("打印e", e)
    values.value = e;
    console.log("回调执行")
}
</script>

<style scoped>

</style>

4.2子组件

<template>
  <div>
    我是子组件,父组件给了我({{ values }})
    <button type="text" @click="tickCallBack">我是按钮</button>
  </div>
</template>

<script setup>
import { ref, defineProps, defineEmits, onMounted } from 'vue';

const props = defineProps( ["datas"]);
const emit = defineEmits(['callBack']);
const values = ref("");
const childValue = ref("给父亲的值");

const tickCallBack = () => {
  emit('callBack', childValue.value);
  console.log("传值给父组件");
};
onMounted(()=> {
    values.value = props.datas;
})
</script>

五、效果演示

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Vue 3中,可以使用props和emit来实现父子组件之间的数据传递。 1. 使用props传递数据给子组件: 在父组件中使用子组件时,通过props属性将数据传递给子组件。在子组件中,可以通过props选项接收并使用这些数据。 父组件: ```vue <template> <div> <child-component :message="parentMessage"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentMessage: '父组件传递的消息' }; } }; </script> ``` 子组件: ```vue <template> <div>{{ message }}</div> </template> <script> export default { props: ['message'] }; </script> ``` 在上面的例子中,父组件通过`:message="parentMessage"`将`parentMessage`的值传递给了子组件的`message`属性。子组件通过`props: ['message']`声明了接收名为`message`的prop,并在模板中使用了该值。 2. 使用emit触发自定义事件传递数据给父组件: 在子组件中,可以通过使用`$emit`方法触发自定义事件,并传递数据给父组件。在父组件中,可以通过监听这些自定义事件来处理传递的数据。 子组件: ```vue <template> <button @click="sendMessage">发送消息给父组件</button> </template> <script> export default { methods: { sendMessage() { this.$emit('message-event', '子组件传递的消息'); } } }; </script> ``` 父组件: ```vue <template> <div> <child-component @message-event="handleMessage"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { handleMessage(message) { console.log('父组件接收到的消息:', message); } } }; </script> ``` 在上面的例子中,子组件通过`this.$emit('message-event', '子组件传递的消息')`触发了名为`message-event`的自定义事件,并传递了消息给父组件。父组件通过`@message-event="handleMessage"`监听了这个自定义事件,并在`handleMessage`方法中接收到了传递的消息。 通过这两种方父子组件可以实现双向的数据传递。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JSU_曾是此间年少

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值