我应该在BehaviorSubject中使用asObservable吗? [英] Should I use asObservable in BehaviorSubject?

查看:177
本文介绍了我应该在BehaviorSubject中使用asObservable吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 BehaviorSubject 中以下代码中的两种方法是什么.

I am wondering what is the two approach in the following code in BehaviorSubject.

据我所知:

asObservable方法不仅将其强制转换为Observable,而且还删除了Observer实现.因此,您将无法呼叫下一个错误&在asObservable()返回的实例上完成.

The asObservable method not only cast it to an Observable, it also removes the Observer implementation. Therefore you are not able to call next, error & complete on the instance returned by asObservable().

但是以下内容也让我感到困惑:

but the following also makes me confused:

通过仅公开为可观察到,您可以使用发出的值,但可以防止从创建此BehaviorSubject的服务外部对BehaviorSubject进行更改.因此,请使用asObservable().

By only exposing asObservable you can consume the values emitted but prevent from making changes to the BehaviorSubject from outside the service where this BehaviorSubject was created. For this reason use asObservable().

这些定义有什么问题吗?

Is there anything wrong with these definitions?

export class DataService {

    // usage I : using getter
    private messageSubject = new BehaviorSubject<any>(undefined);

    getMessage(): BehaviorSubject<any> {
        return this.messageSubject;
    }

    setMessage(param: any): void {
        this.messageSubject.next(param);
    }


    // usage II : using asObservable()
    private messageSubject = new BehaviorSubject<any>(undefined);
    
    currentMessage = this.messageSubject.asObservable();

    setMessage(param: any) {
    this.messageSubject.next(param)
    }
}

以上哪种方法更好用?这两种方法的优缺点是什么?

Which approach above is better to use or what is the pros and cons for these 2 approaches?

更新:上一次我确定以下正确用法:

Update: Last time I finalised the correct usage as the following:

// usage III : using @martin's approach:
private messageSubject = new BehaviorSubject<any>(undefined);
    
public messages$: Observable<any> = this.messageSubject;

//should I set the observable still using the following method without any changing? Or do I need an update?
setMessage(param: any) {
    this.messageSubject.next(param)
}

推荐答案

实际上,建议在TypeScript中执行此操作的方法仅是通过如下类型的类型转换:

Actually, the recommended way of doing this in TypeScript is only by type casting like this:

private messageSubject = new BehaviorSubject<any>(undefined);

public messages$: Observable<any> = this.messageSubject;

这样,是TypeScript编译器不允许您调用 next() error() complete().仅当在纯JavaScript中使用RxJS时,才建议使用 asObservable().例如,在RxJS源代码内部,即使它使用并公开Subjects => ;,也从不使用 asObservable().可以观察到很多.

This way it's TypeScript compiler who won't let you call next(), error() or complete(). Using asObservable() is recommened only when using RxJS in pure JavaScript. For example, internally in RxJS source code it never uses asObservable() even though it uses and exposes Subjects => Observables a lot.

有关更多信息,请参见讨论: https://github.com/ReactiveX/rxjs/pull/2408

For more info see discussion: https://github.com/ReactiveX/rxjs/pull/2408

这篇关于我应该在BehaviorSubject中使用asObservable吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆