我的angular2应用程序中有可观察到的数据。该数据具有两个属性。
{isFetching: false, user: {userID: 1, firstName: "john", lastName: "public"} }.当从服务器检索数据时,isFetching标志为true,而用户对象为null。在我的组件中,当可观测用户对象为null时,我需要通过userID获取其他一些数据,这些数据位于可观测的object.Initially上。在我的代码中,在执行其他操作之前,我必须检查用户是否为null。下面的代码可以工作,但我认为必须有比在订阅块中有if条件更好的方法。
this.store.select<UserProfileState>('user')
.subscribe(user => {
if (!user.isFetching) {
this.store.dispatch(this.informationPanelActions.loadInformationPanel(user.user.clientID, user.user.userID));
}
});任何帮助都是非常感谢的。
发布于 2016-12-02 08:00:48
在使用可观察到的功能时,可以使用筛选器:
this.store.select<UserProfileState>('user')
.filter(user => !user.isFetching)
.subscribe(user => {
this.store.dispatch(this.informationPanelActions.loadInformationPanel(user.user.clientID, user.user.userID));
});https://stackoverflow.com/questions/40927241
复制相似问题