SoFunction
Updated on 2025-03-08

Detailed explanation of the usage of $refs, $emit, $on, $once, $off in vue

1.$refs usage scenario

The parent component calls the child component's method and can pass data.

Parent component:

<div >
  <child-a ref="child"></child-a>
  <button @click="getMyEvent">Click on the parent component</button>
<div>

<script>
  import ChildA from './'
  
  export default{
    components:{
      ChildA
    },
    data(){
      return {
        msg:'I am the data of the parent component'
      }
    },
    methods:{
      getMyEvent(){
        //Call the method of the child component, child is the name given by the above ref, emitEvent is the method of the child component.        this.$()
      }
    }
  }
</script>

Subcomponents:

<template>
  <button>Click me</button>
</template>
<script>
  export default{
    methods:{
      emitEvent(msg){
        ('Received data------'+msg)
      }
    }
  }
</script>

2.Usage of $emit

The child component calls the parent component's methods and passes data.

Subcomponents:

<template>
  <button @click="emitEvent">Click me</button>
</template>
<script>
  export default{
    data(){
      return{
        msg:'I am the data of the child component'
      }
    },
    methods:{
      emitEvent(){
        //Trigger the method through the button's click event, and then use $emit to trigger a custom method of my-event to pass the data.        this.$emit('my-event',)
      }
    }
  }
</script>

Parent component:

<template>  
  <div >
    <child-a @my-event="getMyEvent"></child-a>
    //The parent component executes a method by monitoring the my-event event, and then takes the value passed in the child component.  </div>
</template>
<script>
  import childA from './';
  export default {
    components:{
      childA
    },
    methods:{
      getMyEvent(msg){
       ('Receive data---'+msg);
       //Receive data, I am the data of the subcomponent      }
    }
  }
</script>

3.$on usage scenarios

Brother components pass data to each other.

First create a blank instance of Vue (bridge for sibling components)

import Vue from 'vue';
export default new Vue();

Subcomponent A: Use $emit custom event to bring data over.

<template>
  <div>
    <span>AComponents-{{msg}}</span>
    <input type="button" value="Passing component A data to B" @click="send">
  </div>
</template>
<script>
  import eventBus from './eventBus';
  export default{
    data(){
      return{
        msg:{
          a:'111',
          b:'222'
        }
      }
    },
    methods:{
      send(){
        eventBus.$emit('aevent',)
      }
    }
  }
</script>

Subcomponent B: The receiver receives data through the callback of the custom event by listening to $on

<template>
  <div>
    <span>BComponents,AThe data transmitted is--{{msg}}</span>
  </div>
</template>
<script>
  import eventBus from './'
  export default {
    data(){
      return{
        msg:''
      }
    },
    mounted(){
      eventBus.$on('aevent',(val)=>{// Listen to the event aevent, and the callback function must use the arrow function.        (val);//Print the result; I am the data of component a.      })
    }
  }
</script>

Parent component:

<template>  
  <div>
    <childa></childa>
    <br/>
    <childb></childb>
  </div>
</template>
<script>
  import childa from './';
  import childb from './';
  export default{
    componets:{
      childa,
      childb
    },
    data(){
      return{
        msg:''
      }
    }
  }
</script>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.