DEV Community

Discussion on: Advanced Vue: Controlling Parent Slots (Case Study)

Collapse
 
anduser96 profile image
Andrei Gatej

Interesting problem!

I’d use vm.$root.$on and vm.$root.$emit to be able to control a component from outside.

<template>
  <div>
    <FullPageError />
    <component :is=“crtActionBar” /> 
    <App />
  </div>
</template>

<script>
export default {
 data: () => ({
    crtActionBarName: action1,
  }),

 computed: {
   crtActionBar () {
     return () => import(`path/to/dir/actionBars/${this.crtActionBarName}`)
  }
 },

  created () {
    this.$root.$on(changeActionBar, actionBarName => {
      this.crtActionBarName = actionBarName;
   })
  }
}

</script>

Now from your child component, where you want to change parent’s data from.

created () {
   this.$root.$emit(changeActionBar, action2);
 }