如何将Observable强制转换为FirebaseObjectObservable

时间:2016-10-18 14:09:34

标签: javascript angular firebase rxjs angularfire2

代码

(转换在两种方法中都不起作用)

1. this.af.database.object('/users/'+key)
          .do((something:any)=>console.log(something)) as FirebaseObjectObservable<any>;
2. ..switchMap(()=>this.af.database.object('/users/'+key)) as FirebaseObjectObservable<any>;

问题:

  1. 有什么问题?
  2. 我可以在任何地方用'用户'类型替换'any'而不会丢失任何数据吗?

1 个答案:

答案 0 :(得分:1)

好的解决了它:

1. <FirebaseObjectObservable<any>>this.af.database.object('/users/'+key)
          .do((something:any)=>console.log(something));
2. <FirebaseObjectObservable<any>>..switchMap(()=>this.af.database.object('/users/'+key));

为了替换any,我在我的用户类中添加了一个$key属性,所以现在我可以这样做:

1. <FirebaseObjectObservable<User>>this.af.database.object('/users/'+key)
          .do((something:any)=>console.log(something));
2. <FirebaseObjectObservable<User>>..switchMap(()=>this.af.database.object('/users/'+key));