Angular RxJS-空可观测阵列的测试

Angular RxJS-空可观测阵列的测试,angular,rxjs,ngrx,Angular,Rxjs,Ngrx,我正在学习Angular 2和RxJS,我觉得这可能是最简单的问题。我正在使用商店将“用户”保存到。我想从Typescript端而不是角度模板中测试它是否为空 下面是相关的代码行 public users: Observable<User[]>; public store: Store<AppState>; this.users = this.store.select(state => state.user); if(!this.users) { //DO TH

我正在学习Angular 2和RxJS,我觉得这可能是最简单的问题。我正在使用商店将“用户”保存到。我想从Typescript端而不是角度模板中测试它是否为空

下面是相关的代码行

public users: Observable<User[]>;
public store: Store<AppState>;

this.users = this.store.select(state => state.user);

if(!this.users) {
//DO THIS 
}
公共用户:可观察;
公共商店:商店;
this.users=this.store.select(state=>state.user);
如果(!this.users){
//这样做
}
我尝试了length、==null、==undefined,但找不到如何测试这种看似基本的状态。谢谢你的帮助


请注意,将用户保存和检索到应用商店工作正常

假设
存储
按照您的指示工作,它将返回一个可观察的值。因此,您收到的(
this.users
)实际上不是存储值,而是将输出该值的流

因此,要测试该值是否为空,您不需要测试流本身。相反,您订阅流并观察结果:

public users: User[];

// subscribing will actually trigger the fetch for user data
this.store.select(state => state.user)
          .subscribe(data => this.onUsersEmitted(data));

// executed once user data arrives from the store
public onUsersEmitted(data:User[]){
    this.users = data;
    if(!data || !data.length){ /*empty data arrived*/}
}

这是可以观察到的。它可能是异步的。如果(!this.users).,则不能将其用作
。应该是
this.users.subscribe(users=>{if(!users.length)…})
。谢谢,太好了。显然,我们需要更好地掌握可观测数据。有一点是if条款不起作用。在检查数据时是一个空数组。我用了这个,效果很好。如果(this.users.length==0){@Billyhomebase谢谢你的反馈。更新了我的答案