【发布时间】:2017-12-08 20:38:47
【问题描述】:
我正在浏览 angular-tour-of-heroes 应用程序,我在路由中遇到了 .subscribe 方法。有人能解释一下这里发生了什么吗?
应用链接:https://embed.plnkr.co/?show=preview
在 hero-detail.component.ts 文件中,
ngOnInit(): void {
this.route.paramMap
.switchMap((params: ParamMap) => this.heroService.getHero(+params.get('id')))
.subscribe(hero => this.hero = hero);
}
【问题讨论】:
-
欢迎来到丛林;-) [提示:rxjs Observables]
-
简单来说就是执行观察
-
subscribe(hero => this.hero = hero) -- 如果成功。否则错误处理部分也在那里。示例一是: (error: any) => {console.log(error);} 你必须导入的另一件事: import { Observable, observable } from '../../../node_modules/rxjs';
标签: angular