RxJS 5:我可以创建一个“BehaviorObservable”吗?

RxJS 5: Can I create a `BehaviorObservable`?

我正在为我的应用程序创建一个 class 以保留内部 BehaviorSubject. Is there a way I can expose a corresponding 'BehaviorObservable', which pipes through the values of the subject, but only has the 'read-only' stuff of the Observable 界面? .next().error().complete() 方法只能在内部使用。

这不仅仅是(我相信)使用 Observable.create() 来传递主题的价值观的问题。我的 API 的用户应该 .subscribe() 公开的可观察对象,然后立即获得存储的当前值的回调。

我也许可以一起破解一些东西,但我确定我只是遗漏了一些 RxJS 已经可以做到的东西。

您是否有机会寻找 .asObservable() 方法 - 它存在于 Rxjs v4.1 中。不知道在 Rxjs v5 中是否仍然如此。

根据this,该方法应该包含在 5.0.0-beta.2 版本中。如果功能不存在,我也会在这里引用他们的解决方法:

You can get the same functionality by creating an observable with the private subject's subscribe function:

> const subj = new rx.Subject();
> const exposed = new rx.Observable(fn => subj.subscribe(fn));

关于主题语义的更多细节,你可以看看