我正在研究 NgRx 的替代方案。到目前为止,Akita 是一个不错的选择,但我正在努力处理错误。有时我希望错误出现在应用程序范围内,有时我希望它由组件管理。在下面的示例中,我启动了一个 404 错误,我希望我的组件能够意识到这一点并采取相应的行动,但我不知道该怎么做。你能帮我吗 ?
DummyService(秋田):
get(id: number) {
this.dummyService.loadByID(id).then((dummy: Dummy) => {
this.akitaDummyStore.setDummy(dummy);
})/*.catch((err) => {
alert(err);
Here it's working I go inside catch but I don't want it here I want the catch in the component
})*/;
}
零件 :
constructor(
private akitaDummyQuery: AkitaDummyQuery,
private akitaDummyService: AkitaDummyService
) {
}
ngOnInit() {
this.akitaDummyQuery.selectEntity(666).subscribe(
akDummy => {
if (!akDummy) {
this.akitaDummyService.get(666);
return;
}
this.dummy= Dummy.parse(akDummy.result);
},
error => {
//here I would like to catch the error in the component in order to display a component level error message.
alert(error);
}
);
}
}
我可以像这样处理错误:
this.akitaDummyQuery.selectEntity(id).subscribe(
akDummy => {
if (!akDummy) {
this.akitaDummyService.get(id).catch(err => {
if (err.statusText === 'Not Found') {
}
});
return;
}
this.dummy = Dummy.parse(akDummy.result);
});
但是当我这样做时,akita redux 对象保持在“加载”状态 = true