You can use angular BehaviorSubject for communicating with non related component.
Service file
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class commonService {
private data = new BehaviorSubject('');
currentData = this.data.asObservable()
constructor() { }
updateMessage(item: any) {
this.data.next(item);
}
}
First Component
constructor(private _data: commonService) { }
shareData() {
this._data.updateMessage('pass this data');
}
Second component
constructor(private _data: commonService) { }
ngOnInit() {
this._data.currentData.subscribe(currentData => this.invokeMyMethode())
}
Using above approach you can invoke a method/share data easily with non related components.
More info here
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…