2016-12-02 72 views
0

我在我的angular2应用程序中有可观察的数据。该数据具有2个属性在订阅条件内执行代码

{isFetching: false, user: {userID: 1, firstName: "john", lastName: "public"} }. 

当数据正从服务器isFetching标志检索为真和用户对象为空。在我的组件之一中,我需要通过位于可观察对象上的userID获取一些其他数据。当observable订阅的用户对象为null时。在我的代码中,我必须在执行其他操作之前检查用户是否为空。下面的代码工作,但我认为有更好的方式比有条件里面的订阅块。

this.store.select<UserProfileState>('user') 
     .subscribe(user => { 
      if (!user.isFetching) { 
       this.store.dispatch(this.informationPanelActions.loadInformationPanel(user.user.clientID, user.user.userID)); 
      } 
     }); 

任何帮助表示赞赏。

回答

3

当你与一个可观察的工作,你可以使用过滤器:

this.store.select<UserProfileState>('user') 
    .filter(user => !user.isFetching) 
    .subscribe(user => { 
     this.store.dispatch(this.informationPanelActions.loadInformationPanel(user.user.clientID, user.user.userID)); 
    }); 
+0

谢谢你,成功了! – Yousuf