если вы не отмените подписку BehaviourSubjects, есть вероятность утечки памяти, поэтому вы также можете использовать @ Output
, вот пример
**shared-service**
import { Injectable, Output, EventEmitter } from '@angular/core';
export class SharedService {
@Output() sendData: EventEmitter<any> = new EventEmitter();
}
**component-one**
import { SharedService } from './shared.service';
export class ComponentOneComponent implements OnInit {
constructor(public sharedService: SharedService) {
this.emitData();
}
emitData() {
// this component is emit the data
this.sharedService.sendData.emit('message from component one');
}
}
**component-two**
import { SharedService } from './shared.service';
export class ComponentTwoComponent implements OnInit {
constructor(public sharedService: SharedService) {
}
ngOnInit() {
this.sharedService.sendData.subscribe(data=> {
console.log(data); // here you can get the data from component one
});
}
}
Надеюсьэто поможет вам.