Vue3和Vue2的组件通信

本文详细介绍了Vue3和Vue2中组件间的通信方式,包括props、$emit、expose/ref、$attrs、v-model、provide/inject、Vuex和mitt。Vue3新增了expose和ref的用法,简化了通信过程,而Vue2则有props、$emit/v-on、.sync、v-model、ref、$children/$parent、$attrs/$listeners以及EventBus等多种方式。文中还提到了Vuex作为状态管理器在组件通信中的应用。
摘要由CSDN通过智能技术生成

Vue3 组件通信方式
props
$emit
expose / ref
$attrs
v-model
provide / inject
Vuex
mitt

Vue3 通信使用写法:

  1. props
    用 props 传数据给子组件有两种方法,如下
// Parent.vue 传送
<child :msg2="msg2"></child>
<script setup>
    import child from "./child.vue"
    import {
    ref, reactive } from "vue"
    const msg2 = ref("这是传给子组件的信息2")
    // 或者复杂类型
    const msg2 = reactive(["这是传级子组件的信息2"])
</script>

// Child.vue 接收
<script setup>
    // 不需要引入 直接使用
    // import { defineProps } from "vue"
    const props = defineProps({
   
        // 写法一
        msg2: String
        // 写法二
        msg2:{
   
            type:String,
            default:""
        }
    })
    console.log(props) // { msg2:"这是传级子组件的信息2" }
</script>

  1. $emit
// Child.vue 派发
<template>
    // 写法一
    <button @click="emit('myClick')">按钮</buttom>
    // 写法二
    <button @click="handleClick">按钮</buttom>
</template>
<script setup>
    
    // 适用于Vue3.2版本 不需要引入
    // import { defineEmits } from "vue"
    // 对应写法一
    const emit = defineEmits(["myClick","myClick2"])
    // 对应写法二
    const handleClick = ()=>{
   
        emit("myClick", "这是发送给父组件的信息")
    }
</script>

// Parent.vue 响应
<template>
    <child @myClick="onMyClick"></child>
</template>
<script setup>
    import child from "./child.vue"
    const onMyClick = (msg) => {
   
        console.log(msg) // 这是父组件收到的信息
    }
</script>

  1. expose / ref
    父组件获取子组件的属性或者调用子组件方法
// Child.vue
<script setup>
    // 适用于Vue3.2版本, 不需要引入
    // import { defineExpose } from "vue"
    defineExpose({
   
        childName: "这是子组件的属性",
        someMethod(){
   
            console.log("这是子组件的方法")
        }
    })
</script>

// Parent.vue  注意 ref="comp"
<template>
    <child ref="comp"></child>
    <button @click="handlerClick">按钮</button>
</template>
<script setup>
    import child from "./child.vue"
    import {
    ref } from "vue"
    const comp = ref(null)
    const handlerClick = () => {
   
        console.log(comp.value.childName) // 获取子组件对外暴露的属性
        comp.value.someMethod() // 调用子组件对外暴露的方法
    }
</script>

  1. attrs
    attrs:包含父作用域里除 class 和 style 除外的非 props 属性集合
// Parent.vue 传送
<child :msg1="msg1" :msg2="msg2" title="3333"></child>
<script setup>
    import child from "./child.vue"
    import {
    ref, reactive } from "vue"
    const msg1 = ref("1111")
    const msg2 = ref("2222")
</script>

// Child.vue 接收
<script setup>
    import {
    defineProps, useAttrs } from "vue"
    // 3.2版本不需要引入 defineProps,直接用
    const props = defineProps({
   
        msg1: String
    })
    // 适用于 Vue3.2版本
    const attrs = useAttrs()
    console.log(attrs
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值