If the menu component is the child of the other component, you can emit an Event from it and listen to it on the other component.
Example:
foo.ts
import { Component, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'foo',
template: `<button (click)="OnMyEvent()">Click to fire Event</button>`
})
export class Foo {
@Output() myEvent = new EventEmitter();
usefullInformation = {mymessage: 'It works'};
OnMyEvent() {
this.myEvent.emit(this.usefullInformation);
}
}
bar.ts
import { Component } from '@angular/core';
@Component({
selector: 'bar',
template: `
<foo (myEvent)="handleMyEvent($event)"></foo>
<div>
I listen to an Event!
{{ usefullInformation | json }}
</div>`
})
export class Bar {
usefullInformation;
handleMyEvent(eventObject) {
this.usefullInformation = eventObject;
console.log(JSON.stringify(this.usefullInformation));
}
}
Example Plunk: http://embed.plnkr.co/jO3Ja1qVRqssmKu8YPbF/
Hint: Angular EventEmitter events do not bubble. This is for the same reason the official guide (https://angular.io/guide/component-interaction) only handles parent<->child interaction. Coders with limited software engeneering skills might wonder about it or use workarounds like dispatchEvent(event)
. But the stream of information is easier to understand if it is limitied by the DOM-Structure. In a medium sized app, debugging would get hard if every component is able to "talk" to every other component. If components with no parent<->child relation need to "talk", there is always the way to wire through a common ancestor:
<c-ancestor>
<foo></foo>
<bar></bar>
</c-ancestor>
Or one can use more advanced architecture approaches like redux.