Angular 理解角度2中的可观察物和观察者
下面是Angular 2服务中的一个方法,它返回“Category”对象数组的“Observable” 风格2Angular 理解角度2中的可观察物和观察者,angular,rxjs,Angular,Rxjs,下面是Angular 2服务中的一个方法,它返回“Category”对象数组的“Observable” 风格2 this.categories$ = this.appService.GetCategories(); // returns an Observable<Array<Category>> this.categories$=this.appService.GetCategories();//返回一个可观察的 并使用“异步”管道访问模板中的数据 现在我的
this.categories$ = this.appService.GetCategories(); // returns an Observable<Array<Category>>
this.categories$=this.appService.GetCategories();//返回一个可观察的
并使用“异步”管道访问模板中的数据
现在我的问题是,在样式1的情况下,我能够在出现错误时显示错误警报,但是如果我使用样式2,如何显示自定义错误警报。您可以有多个订阅方订阅此。类别$-可以在模板中单独执行错误处理,但这将发送HTTP请求两次,当然,除非您在订阅之前共享可观察内容。@echonax,您提供的链接很有用
this.appService.GetCategories().subscribe({next: (data) => {
this.appService.categories = data;
this.categories = this.appService.categories
},
error: (err) => {
this.toaster.error(err);
}
});
this.categories$ = this.appService.GetCategories(); // returns an Observable<Array<Category>>