在Angular 2应用程序中页面加载时组合两个Observable

时间:2017-08-22 21:29:05

标签: javascript angular rxjs observable

我在Angular应用程序中使用observable来提取数据,然后处理导航。但目前发生的是第一个订阅到来,数据相应调整。然后,在短暂的延迟之后,第二个订阅开始,处理过滤器和分页,并且数据发生变化。我想要做的是将这两者合并为一个动作,以便数据只加载一次。

这就是我目前所拥有的:

ngOnInit()
{
    this.dataService.getBySelection(this.page, this.pagesize, this.body, this.type)
        .subscribe(resRecordsData => {
                    this.records = resRecordsData;
                },
                responseRecordsError => this.errorMsg = responseRecordsError
        );

    this.route.params.subscribe(
        (params: any) => {
            this.page = params['page'];
            this.type = params['type'];
        }
        );
        this.router.navigate(
            ['/consulting', { page: this.page || 1 , type: this.type }]);
}

更新:根据评论者的建议和咨询文档,我能够做到这一点:

Observable.combineLatest(
    this.dataService.getBySelection(this.page, this.pagesize, this.body, this.type),
    this.route.params)
    .subscribe(result => console.log('combineLatest: ', result));

然后我在控制台中获取正确的数据。所以我应该好好去。

0 个答案:

没有答案