私はこのような件名サービスを持っています。角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();
}
問題:子コンポーネントの1が最初にリロードページに開かれたとき すべてが良いです。しかし、子コンポーネントのタブを変更して、最初に開いた子コンポーネントをもう一度見直しても、サブスクライブされたデータは取得されません。しかし、ngOnInit
機能が再び機能します。また、getMessage() in service
も機能します。子コンポーネントのsubscribeコールバックのみがデータを再度取得しません。