SoFunction
Updated on 2025-04-11

Summary of the methods and example usage of vue3 component communication

vue3 component communication methods are as follows

  • props
  • $emit
  • $expose / ref
  • $attrs
  • v-model
  • provide / inject
  • Vuex
  • mitt

props

<child :msg2="msg2" />
<script setup>
    const props = defineProps({
        // Writing method one        msg2:String
        // Write method two        msg2:{
            type:String,
            default:''
        }
    })
    (props) // {msg2:'This is the information of the pass-through subcomponent 2'}</script>

$emit

//
<template>
    // Writing method one    <div @click="emit('myclick')">Button</div>
    // Write method two    <div @click="handleClick">Button</div>
</template>
<script setup>
    // Method 1    const emit = defineEmits(['myClick'],['myClick2'])
    // Method 2    const handleClick = () => {
        emit('myClick','This is the message sent to the parent component');
     }
      
     // Method 2 does not apply to vue3.2, the useContext() used has been discarded     import { useContext } from 'vue'
     const { emit } = useContext()
     const handleClick = () => { 
      emit('myClick','This is the message sent to the parent component'   
     }
</script>
 
// Response<template>
    <child @myClick="onMyClick"></child>
</template>
<script setup>
    import child from "./"
    import onMychilk = (msg) => {
        (msg) // Message received by the parent component    }
</script>

expose / ref

Parent component obtains the properties of the child component or calls the child component method

<script setup>
    // Method 1: UseContext() has been discarded after vue3.2    import { useContext } from 'vue'
    const ctx = useContext()
    // Exposing the attributes to the outside can be done, etc.    ({
        childName: 'This is the attribute of the subassembly',
        someMethod(){
        ('This is the method of the child component')
        }
    })
</script>
 
// Note ref="comp"<template>
    <child ref="comp"></child>
    <button @click="handleClick">Button</button>
</template>
<script>
    import child from './'
    import { ref } from 'vue' 
    const comp = ref(null)
    const handleClick = () => {
        ()
        () // Call the method of exposing subcomponents to the outside    }
</script>

attts

attrs: Contains a collection of non-props attributes except class and style in the parent scope

// Parent component<child :msg1="msg1" :msg2="msg2" title="3333"></child>
<script setup>
    import child from './'
    import { ref,reactive } from 'vue
    const msg1 = ref('111')
    const msg2 = ref('222')
</script>
 
// Subcomponents<script setup>
    import { defineProps,useContext,useAttars } from 'vue'
    const props = defineProps({
        msg1: String
    })
     
    // Method 1    const ctx = useContext()
    () // {msg2:'222',title:'333'}
     
    // Method 2    const attrs = useAttars()
    (attars)  // {msg2:'2222',title:'3333'}
</script>

v-model

Can support multiple data bidirectional binding

<child v-model:key="key" v-modle:value="value" />
<script>
    import child from './'
    import { ref,reactive } from 'vue'
    const key = ref('111')
    const value = ref('222')
</script>
 
//Subcomponent<template>
   <button @click="handleClick"></button>
</template>
<script setup>
    // Method 1 v3.2 has been removed    import { useContext } from 'vue'
    const { emit } = useContext()
     
    // Method 2    import { defineEmits } from 'vue'
    const emit = defineEmits(['key','value'])
     
    //usage    const handleClick = () => {
        emit('update:key','New key')
        emit('update:value','New value')
    }
</script>

provide / inject

provide/inject for dependency injection: allows us to specify the data we want to provide to the descendant component inject: accept the data we want to add to this component in any descendant component. No matter how deep the component is nested, it can be used directly

// Parent component<script setup>
    import { provide } from 'vue'
    const name = provide('name')
    ('name','Mu Hua')
</script>
//Subcomponent<script setup>
    import { inject } from 'vue'
    const name = inject('name')
    (name) //Mu Hua</script>

Vuex

//store/
import { createStore } from 'vuex'
export default createStore({
    state:{count:1},
    getters:{
        getCount:state=>
    },
    mutations:{
        add(state){
            ++
        }
    }
   })
 // 
 import { createApp } from 'vue'
 import APP from './'
 import store from './store'
 createApp(APP).use(store).mount("#app")
  
 // Use directly <template>
     <div>
         {{ $ }}
     </div>
     <button @click="$('add')">
     </button>
 </template>
  
 // Get <script setup>
     import { useStore,computed } from 'vuex'
     const store = useStore()
     ()
      
     const count = computed (()=>)
     (count)
 </script>

mitt

EventBus is no longer cross-component communication in Vue3, an alternative, but the principle and method EventBus are the same
Installation method npm i mitt -S

Package


import mitt from 'mitt'
const mitt = mitt()
export default mitt

Use between components

// Component A<script setup>
    import mitt from './mitt'
    const handleClick = () => {
        ('handleChange')
    }
</script>
// Component B<script setup>
import mitt from './mitt'
import { onUnmounted } from 'vue'
const someMethod = () => {...}
('handleChange',someMethod)
onUnmounted(()=>{
    ('handleChange',someMethod)
})
</script>

This is the article about the summary of vue3 component communication methods and example usage. For more information about vue3 component communication, please search for my previous article or continue browsing the related articles below. I hope you will support me in the future!