I have in Layout.vue
to components one TheSidebar
second TheHeader
, there is a button in TheHeader
to open the sidebar in TheSidebar
component.
I need to when I click the button in header open the sidebar:
My try:
in TheHeader
:
methods: {
openSidebar() {
this.$root.$emit("open-sidebar");
},
},
in TheSidebar
data() {
return {
sidebarOpen: false,
};
},
mounted() {
this.$root.$on("open-sidebar", (this.sidebarOpen = true));
},
I'm using VUE 3 so I got this error in console: TypeError: this.$root.$on is not a function
so How can communicate ?
you can use something like tiny emitter it works fine and doesn't care about parent child relationship
var emitter = require('tiny-emitter/instance');
emitter.on('open-sidebar', ({isOpen}) => {
//
});
emitter.emit('open-sidebar', {isOpen : true} );
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments