vue3中父子间的通信(双向绑定传递的参数)

一、.常用的父子通信

(1)父传子

父组件fatherPage.vue传递数据
<!-- 父组件father.vue -->
<template>
    <div>
        <div>我是父组件fatherPage</div>
        <ChildPage :dialogHct="dialogHctVisible"></ChildPage>
    </div>
</template>
<script setup>
import ChildPage from "./components/ChildPage.vue";
import {ref} from 'vue';

const dialogHctVisible = ref(true)
</script>
子组件childPage.vue接收数据

<template>
    <div>我是子组件ChildPage</div>
</template>
import {defineProps,onMounted,ref} from 'vue';

const props = defineProps({
    dialogHct:  Boolean,
});

const params = ref("") // 用于接收参数

onMounted(()=>{
    console.log("props",props); // 输出父组件传递过来的所有数据
    params.value = props.dialogHct; // 接收参数
})

(2)子传父

子组件childPage.vue传递数据
<template>
<el-dialog v-model="dialogHct" title="子组件弹框" @close="handleCloses">
    <div>我是子组件ChildPage</div>
</el-dialog>
</template>

const emit = defineEmits(['handleCloses'])
cosnt text = ref("子传父")

function handleCloses() { // 子传父
    emit("handleCloses",text.value)
}
父组件fatherPage.vue接收数据
<!-- 父组件father.vue -->
<template>
    <div>
        <div>我是父组件fatherPage</div>
        <ChildPage @handleClose="handleParams"></ChildPage>
    </div>
</template>
<script setup>
import ChildPage from "./components/ChildPage.vue";
import {ref} from 'vue';

const dialogHctVisible = ref(true)
function handleParams(params){ // 参数就是子组件传递过来的数据
    dialogHctVisible.value = params;
}
</script>

二、.简化的父子通信(双向绑定传递的参数)

(1)父组件fatherPage.vue传递数据

<!-- 父组件father.vue -->
<template>
    <div>
        <div>我是父组件fatherPage</div>
        <ChildPage v-model:dialogHct="dialogHctVisible"></ChildPage>
    </div>
</template>
<script setup>
import ChildPage from "./components/ChildPage.vue";
import {ref} from 'vue';

const dialogHctVisible = ref(true)
</script>

(2)子组件childPage.vue接收数据

<template>
<el-dialog v-model="dialogHct" title="子组件弹框" @close="handleCloses">
    <div>我是子组件ChildPage</div>
</el-dialog>
</template>
import {defineProps,toRefs,defineEmits,onMounted,ref} from 'vue';

const props = defineProps({
    dialogHct:  Boolean,
});
let { dialogHct } = toRefs(props); // 如果需要改变dialogHct,就不能使用const
const emit = defineEmits(['update:dialogHct'])
const params = ref("") // 用于接收参数

onMounted(()=>{
    console.log("props",props); // 输出父组件传递过来的所有数据
    // 两种接收参数的方式
    params.value = props.dialogHct; // 第一种:使用'props.'点出对应的属性来接收传递过来的参数
    params.value = dialogHct; // 第二种:使用toRefs(props)将props结构成普通对象来接收传递过来的参数
    // 第一种如果对props.dialogHct重新赋值页面是能响应变化的,但是不够简化;
    // 第二种够简化,但是如果对dialogHct重新赋值页面是不会跟其响应变化的;
    // 所以我们一般都是使用toRefs()将props解构出来,再重新赋值到新的变量(params)就既能简洁又能响应
})

function handleCloses() { // 子传父
    props.dialogHct = false;
    emit("update:dialogHct",false); // 只要执行了该方法,父组件绑定变量(dialogHctVisible)就会赋值为false
}

(3)说明

1.如果有多个数据需要传递到子组件,那么只需要在子标签上多添加一个v-modle

<DialogHctTable v-model:dialogHct="dialogHctVisible" v-model:dialogHct2="dialogHctVisible2"></DialogHctTable>

2.因为是使用 v-model:dialogHct="dialogHctVisible",双向绑定了dialogHctVisible变量,所以在子组件中只要是emit("update:dialogHct",传递的数据)就会将传递的数据赋值给dialogHctVisible,无需使用事件来触发

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue是一款流行的前端框架,它支持自定义组件,并提供了v-model指令来实现双向数据绑定。同时,Vue也提供了多种方式来实现父子组件的同步通信。 对于自定义组件的v-model双向绑定来说,最简单的方法就是在组件的props定义一个value属性,并将其绑定到父组件的data的属性。然后,组件通过$emit('input', value)触发一个input事件来改变父组件的数据。例如: 父组件: ```vue <template> <div> <custom-input v-model="message"></custom-input> </div> </template> <script> import CustomInput from './CustomInput.vue'; export default { components: { CustomInput }, data() { return { message: 'Hello world!' }; } }; </script> ``` 组件: ```vue <template> <div> <input :value="value" @input="$emit('input', $event.target.value)"> </div> </template> <script> export default { props: { value: { type: String, required: true } } }; </script> ``` 除了上述方式外,还可以通过自定义指令、混入等方式来实现自定义组件的v-model双向绑定。 对于父子组件的同步通信Vue提供了多种方式来实现,例如: 1. props和$emit 在父组件通过props将数据传递组件,在组件通过$emit触发一个事件并将数据传递回父组件。例如: 父组件: ```vue <template> <div> <child-component :message="message" @update-message="updateMessage"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { message: 'Hello world!' }; }, methods: { updateMessage(value) { this.message = value; } } }; </script> ``` 组件: ```vue <template> <div> <input :value="message" @input="updateMessage($event.target.value)"> </div> </template> <script> export default { props: { message: { type: String, required: true } }, methods: { updateMessage(value) { this.$emit('update-message', value); } } }; </script> ``` 2. $parent和$children 在父组件通过$children获取所有组件,然后通过$parent获取父组件,从而实现父子组件通信。例如: 父组件: ```vue <template> <div> <child-component></child-component> <button @click="sendMessage">Send message to child</button> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { sendMessage() { this.$children[0].sendMessage('Hello child'); } } }; </script> ``` 组件: ```vue <template> <div> <p>{{ message }}</p> </div> </template> <script> export default { data() { return { message: '' }; }, methods: { sendMessage(value) { this.message = value; } } }; </script> ``` 除了上述方式外,还可以通过$refs、事件总线、Vuex等方式来实现父子组件的同步通信。需要根据具体的场景来选择合适的方式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值