【发布时间】:2020-03-24 20:46:26
【问题描述】:
如果记录存在,我正在调用我的服务方法来获取记录并使用布尔值更新状态 (ngsx)。如果记录存在,我将 state 中的 agreementExists 值设置为 true,否则将其设置为 false。我正在尝试在应用程序组件中加载模式对话框并订阅 observable 以检查记录是否存在。在应用程序组件第一次加载时,agreementModalStatus 本身是未定义的,并且仅在刷新后才被初始化。有人可以告诉我问题是什么。我该如何解决这个问题
应用组件
public ngOnInit() {
this.agreementModalState$.pipe(first()).subscribe(agreementModalStatus => {
console.log('Inside ngOnit- AppComponent');
console.log(agreementModalStatus.agreementExists);
console.log(agreementModalStatus.isInteracted);
if (agreementModalStatus !== undefined && agreementModalStatus.isInteracted === false && agreementModalStatus.agreementExists) {
this.loadModalDialog();
}
});
}
public loadModalDialog() {
this.modal = this.modalService.open(AgreementComponent, { size: 'lg', centered: true , backdrop: 'static'});
}
组件
public getOutstandingAgreements(Id: number) {
this.loading = true;
this.agreementsService
.getOutstandingAgreements(Id)
.subscribe((data: AgreementsModel[]) => {
if (data.length > 0) {
console.log('IF block');
this.store.dispatch(new SetAgreementStatus({ isInteracted: false , agreementExists: true}));
this.myData = data;
this.agreementData = this.myData[0].data;
this.agreementLength = this.myData.length;
this.SetWarningMessage(0);
this.loading = false;
} else {
console.log('Else block');
this.store.dispatch(new SetAgreementStatus({ isInteracted: false , agreementExists: false}));
}
});
}
【问题讨论】: