【问题标题】:Use in html the async pipe => ¿BehaviorSubject or Observable?在 html 中使用异步管道 => ¿BehaviorSubject 或 Observable?
【发布时间】:2019-02-22 08:09:51
【问题描述】:

我有一个疑问,我正在从事的项目,我看到了这个:

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

然后,在 html 模板中:

{{ isReady$ | async}}

为什么不将behaviourSubject 定义为public,并在带有异步管道的html 中直接使用它?将该行为主题作为可观察对象返回以在 html 中使用它有什么区别/好处?

【问题讨论】:

    标签: angular observable behaviorsubject


    【解决方案1】:

    BehaviorSubject(_isReady$) 私有的原因是应该只允许特定服务发出 observable(不应允许其他组件或服务这样做)。

    但其他人只能使用可公开访问的 Observable subscribeIsReady$ 对其进行处理

    【讨论】:

    • 您说得对,非常感谢您的解释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 2016-09-01
    • 2018-01-23
    • 2023-03-29
    • 2019-08-02
    • 1970-01-01
    相关资源
    最近更新 更多