如何从 AngularFire2 中的 BehaviorSubject 获取当前值?

How to get current value from a BehaviorSubject in AngularFire2?

我正在使用 AngularFireDatabase 来获取我的数据库的一部分,就像这样 db.object('/accounts/'+this.cid); 其中 this.cid 是一个可以更改的 BehaviorSubject。我是不是用错了方法?

cid: BehaviorSubject<string>;
account: FirebaseObjectObservable<any>;
constructor(private db: AngularFireDatabase) {
  this.cid = new BehaviorSubject<string>('fs2ejD4ds');
  this.account = db.object('/accounts/'+this.cid);

基本上每当我 运行 this.cid.next('fhEj2jd') 我希望它改变我们试图在 Firebase 中引用的对象。

要从 BehaviorSubject 获取当前值,您可以调用它的 getValue 方法,请参阅 docs.

this.account = db.object('/accounts/'+this.cid.getValue());

但请记住,每次 this.cid 的当前值发生变化时,您都必须覆盖原始可观察对象 this.account。如果你只是订阅 this.account,那么你必须在用新的 observable 覆盖它之前取消订阅(对于取消订阅,你可以使用异步管道自动完成)。

参考简单demo.