在 html 中使用异步管道 => ¿BehaviorSubject 或 Observable?
Use in html the async pipe => ¿BehaviorSubject or Observable?
我有疑问,我正在做的项目,我看到了这个:
private _isReady$: BehaviorSubject<boolean> = new <boolean>(false);
get IsReady$(): Observable<boolean> {
return this._isReady$.asObservable();
}
然后,在 html 模板中:
{{ isReady$ | async}}
为什么不将 behaviorSubject 定义为 public,并直接在带有异步管道的 html 中使用它? difference/benefit 返回该行为主体作为可观察对象以在 html 中使用它是什么?
BehaviorSubject
(_isReady$
) 私有的原因是只应允许特定服务发出可观察对象(不应允许其他组件或服务这样做)。
但其他人只能 subscribe
使用可公开访问的 Observable IsReady$
我有疑问,我正在做的项目,我看到了这个:
private _isReady$: BehaviorSubject<boolean> = new <boolean>(false);
get IsReady$(): Observable<boolean> {
return this._isReady$.asObservable();
}
然后,在 html 模板中:
{{ isReady$ | async}}
为什么不将 behaviorSubject 定义为 public,并直接在带有异步管道的 html 中使用它? difference/benefit 返回该行为主体作为可观察对象以在 html 中使用它是什么?
BehaviorSubject
(_isReady$
) 私有的原因是只应允许特定服务发出可观察对象(不应允许其他组件或服务这样做)。
但其他人只能 subscribe
使用可公开访问的 Observable IsReady$