我只是通过阅读v.4.0的教程来学习Angular。我刚刚参加了该教程的Section 6(路由),并且我理解了subscribe
方法。我会很乐意为你解释一下。有人可以解释Angular订阅方法
据我所知,ngOnInit()
被调用一次,这就是为什么我们在这里使用subscribe()
。但是,什么事件使subscribe()
被触发?只有当再次请求包含特定HeroDetailComponent
的页面时才会触发它。为了我的理解,它必须附加到ActivatedRoute.params上的某种'onChange'事件,并在用户请求同一页面时触发(包含HeroDetailComponent
)。
一旦ActivatedRoute.params
改变会发生什么呢?我的意思是 - 如果ngOnInit()
只执行一次,this.hero
如何分配一个新值。我很好奇它是如何知道执行this.heroService.getHero(+params['id'])
并将返回值分配给this.hero
?从教程here
// mock-heroes.ts
export const HEROES: Hero[] = [
{ id: 11, name: 'Mr. Nice' },
{ id: 12, name: 'Narco' },
{ id: 13, name: 'Bombasto' },
{ id: 14, name: 'Celeritas' },
{ id: 15, name: 'Magneta' },
{ id: 16, name: 'RubberMan' },
{ id: 17, name: 'Dynama' },
{ id: 18, name: 'Dr IQ' },
{ id: 19, name: 'Magma' },
{ id: 20, name: 'Tornado' }
];
// hero.service.ts
@Injectable()
export class HeroService {
getHeroes(): Promise<Hero[]> {
return Promise.resolve(HEROES);
}
getHero(id: number): Promise<Hero> {
return this.getHeroes()
.then(heroes => heroes.find(hero => hero.id === id));
}
// hero-detail.component.ts
@Component({...})
export class HeroDetailComponent implements OnInit{
constructor(
private heroService: HeroService,
private route: ActivatedRoute,
private location: Location
) {}
ngOnInit(): void {
this.route.params
.switchMap((params: Params) => this.heroService.getHero(+params['id']))
.subscribe(hero => this.hero = hero);
}
编辑
完整的源代码: 我刚刚发现了一个great article如果有人有问题理解这些概念,这可能是有用的。
请参阅:http://xgrommx.github.io/rx-book/content/observable/observable_instance_methods/subscribe.html和https://xgrommx.github.io/rx-book/content/getting_started_with_rxjs/creating_and_querying_observable_sequences/ creating_and_subscribing_to_simple_observable_sequences.html – wannadream