제목 서비스가 있습니다.탭 2 변경 후 다시 방문하여 각도 2로 구독하십시오.
private subject = new Subject<any>();
sendMessage(message: any) {
this.subject.next(message);
}
clearMessage() {
this.subject.next();
}
getMessage(): Observable<any> {
return this.subject.asObservable();
}
그리고 부모 구성 요소에서 메시지를 먹이
참고 : 내 부모 - 자식 구성 요소는 다음 router-outlet
private getMerchantDetail() {
let data = {
Id: this.merchantId,
}
this.merchantsService.getMerchants(data)
.then((res) => {
// console.log(res)
if (res.Success) {
this.merchant = res.Data[0];
this.merchantListDetailService.sendMessage(res.Data[0]); -> here
}
})
.catch((err) => { })
}
그리고 모든 하위 구성 요소의 데이터를 가입을 통해 연결;
ngOnInit() {
this.merchantSubscribe = this.merchantListDetailService.getMessage()
.subscribe(merchant => {
console.log(merchant)
this.merchant = merchant;
this.getMerchantPaymentChannels();
})
}
ngOnDestroy() {
this.merchantSubscribe.unsubscribe();
}
문제 : 자식 요소 중 하나가 처음 리로디드 페이지 열 때 모든 것이 좋다. 그러나 하위 구성 요소의 탭을 변경하고 처음에 열린 하위 구성 요소를 다시 방문하면 가입 된 데이터를 가져 오지 못합니다. 그러나 ngOnInit
기능이 다시 작동합니다. 또한 getMessage() in service
도 작동합니다. 하위 구성 요소 subscribe 콜백 만 데이터를 다시 수신하지 않습니다.