Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 理解角度2中的可观察物和观察者_Angular_Rxjs - Fatal编程技术网

Angular 理解角度2中的可观察物和观察者

Angular 理解角度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();//返回一个可观察的 并使用“异步”管道访问模板中的数据 现在我的

下面是Angular 2服务中的一个方法,它返回“Category”对象数组的“Observable”

风格2

    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>>