多次调用链接的Rx调用

时间:2016-12-11 15:36:41

标签: angular typescript rxjs

我需要按顺序执行http get调用。这个How to chain Http calls in Angular2对如何做到这一点有很好的解释。然而,每当我重新审视这条路线时,我的观察结果在每次访问后被称为2次。如此多的重复http调用并不好。如何确保只进行一次通话。

取消订阅破坏似乎也无济于事。

ngOnInit() {
    let counter1 = 0, counter2 = 0;
    console.log('MyBiraComponent ngOnInit');
    let uk = this.us.get();
    this.u = uk.map(person => {
        console.log('line 47, Person Id: ', ++counter1);
        return person.id;
    }).flatMap(id => {
        console.log('line 48 - Person id: ', person.id);
        return this.bu.get(id);
    }).subscribe(res => {
        console.log(res);
        });
}




ngOnDestroy() {
    this.u.unsubscribe();
    console.log('MyBiraComponent Destroyed');
  }

首次访问路线

enter image description here

第二次访问路线

enter image description here

第3次访问路线

enter image description here

0 个答案:

没有答案
相关问题