简体   繁体   English

在html中使用异步管道=>¿BehaviorSubject或Observable?

[英]Use in html the async pipe => ¿BehaviorSubject or Observable?

I have a doubt, I the project I'm working on, I'v seen this: 我有一个疑问,我正在从事的项目中,我已经看到了:

private _isReady$: BehaviorSubject<boolean> = new <boolean>(false);
get IsReady$(): Observable<boolean> {
    return this._isReady$.asObservable();
}

And then, In the html template: 然后,在html模板中:

{{ isReady$ | async}}

Why not define the behaviorSubject as public, and use it direcly in the html with the async pipe? 为什么不将behaviorSubject定义为public,并通过异步管道在html中直接使用它呢? What is the difference/benefit returning that behaviour subject as an observable to use it in the html? 返回该行为主题作为可观察到的在html中使用的区别/好处是什么?

The reason being BehaviorSubject ( _isReady$ ) private is that only a particular service should be allowed to emit the observable (no other components or service should be allowed to do so). 之所以将BehaviorSubject_isReady$_isReady$私有,是因为仅应允许特定服务发出可观察对象(不允许其他组件或服务这样做)。

But others can only subscribe to it using a publicly accessible Observable IsReady$ 但是其他人只能使用可公开访问的Observable IsReady$ subscribe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM