Angular2 activatedRoute queryParam从subscribe获取未定义的参数

时间:2018-02-27 18:35:08

标签: angular

在我的组件中,在ngOnInit中,我订阅了activedRoute.queryParam来获取查询参数。但有时它会得到未定义的值,这会破坏后续的API调用,因为undefined会传递给API。

    this.activatedRoute.queryParams.subscribe(params => {     
        this.personId = params['personId'];
    });

在进行API调用之前,我应该如何确保首先获得personId。 任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:2)

您可以按如下方式使用filter operator,以确保仅在值存在时进行订阅:

this.activatedRoute.queryParams.filter(params => params['personId'])
.subscribe(params => {     
   this.personId = params['personId'];
   // make your other API calls..   

});
相关问题