我从MerchantComponent
并从EventEmitterService
通过MerchantPaymentChannelComponent
订阅,当路由直接打开此页面时,它就可以了。但是你看到还有其他标签,每个标签都有自己的组件。当我将标签更改为不同的标签时,请回到此MerchantPaymentChannelComponent
,它不会再次订阅。
注意:我在NgOnDestroy
活动
这是代码;
MerchantListDetailService(通过EVENTEMITTER共享服务)
export class MerchantListDetailService {
@Output() emittedMerchant: EventEmitter<any> = new EventEmitter();
constructor() {}
emitMerchant(data) {
this.emittedMerchant.emit(data);
}
getEmittedValue() {
return this.emittedMerchant;
}
}
MerchantComponent(触发此组件的EMIT)
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.emitMerchant(res.Data[0]);
}
})
.catch((err) => { })
}
MerchantPaymentChannelComponent(从此组件订阅)
ngOnInit() {
this.merchantSubscribe = this.merchantListDetailService.getEmittedValue()
.subscribe(merchant => {
this.merchant = merchant;
this.getMerchantPaymentChannels();
})
}
ngOnDestroy() {
this.merchantSubscribe.unsubscribe();
}