How can I emit from component and listen from another one?

sime

I have in Layout.vue to components one TheSidebar second TheHeader, there is a button in TheHeader to open the sidebar in TheSidebarcomponent.

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 ?

Aaron Saunders

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.

edited at
0

Comments

0 comments
Login to comment

Related

How can I pass an *ngIf value from one component to another?

How can I pass data from one component to another for edit form?

How do I pass redux store from one component to another?

How can I call a function from one component to another? React

How can I correctly pass state as props from one component to another?

How can I pass props from one component to another using Link and router in react

How can i pass data of clicked row from one component to another component without passing to the url

How can I pass the state from one component to another and how can I modify the state?

How can I read a variable from another component in React?

How can I update a hook from one component and have it update another component as a result?

VueJS - How can i refresh a table component from another component?

React - How can I pass API data from one component to another in a different js file?

How can I append data from one spreadsheet to another one?

How can I listen to one event from two template handlers?

how can I pass a C# component from one gameobject to another in Unity3d

In Electron version 1.X, how can I emit an event from one BrowserWindow and consume it in another BrowserWindow?

How can I access a component(for example a timepicker) in one activity from another activity in Android?

Can't emit from one child to another

How can I pass data from a component to another component on vue?

How can I pass value from one component from another component in Angular8 (Independent components)

How do I pass a variable from one component to another?

How do I persist formdata from one component to another in angular?

How can I pass state from one component to set another state?

how can i use NavLink to redirect from component to another component

How can I simply pass data from a component one page to a component on another page in React/Next.js?

How can I pass props from one component to another in typescript?

How can i call a modal component from another page

How can I re-render one component from another component in React?

How do I pass useState from one component to another?

TOP Ranking

HotTag

Archive