[Vuejs]-Vuejs: Access a Parent Component Method from Child Component

3πŸ‘

βœ…

It’s not a good practice to use $parent, but instead of that you could use emit to send an event from the child component to run some method in parent one :

in the Child component emit the event with your payload :

    swapComponent: function (component) {
      this.$emit("swap-component",this.components[component])
    }

in parent one:


 <DrawerComponent @swap-component="displayComponent"></DrawerComponent>

and change div to component in


<div ref="ComponentDisplay" :is="currentComponent"></div>

Leave a comment