You can do it via service, which will be injected in both components
Service could be like this one
@Injectable()
class MyDataService implements IMyDataService {
private whenMessageRecieved$: Subject<MyDataType> =
new ReplaySubject(1);
constructor(
) {
}
public notify(someData: MyDataType): void {
this.whenMessageRecieved$.next(someData);
}
public whenMessageRecieved(): Observable<MyDataType> {
return this.whenMessageRecieved$.asObservable();
}
}
ComponentB should subscribe to whenMessageRecieved() and componentA should call notify with strict params. In nearest future this service will help you migrate methods from componentB
But still - it would be better to store common logic somewhere else, not in componentB
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…