我应该在 BehaviorSubject 中使用 asObservable 吗?
Should I use asObservable in BehaviorSubject?
我想知道 BehaviorSubject
.
中以下代码中的两种方法是什么
据我所知:
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().
但是下面的也让我很疑惑:
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().
这些定义有什么问题吗?
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)
}
}
以上哪种方法更好用,或者这两种方法的优缺点是什么?
更新:上次确定正确用法如下:
// 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 中推荐的做法是像这样进行类型转换:
private messageSubject = new BehaviorSubject<any>(undefined);
public messages$: Observable<any> = this.messageSubject;
这样,TypeScript 编译器将不允许您调用 next()
、error()
或 complete()
。只有在纯 JavaScript 中使用 RxJS 时才推荐使用 asObservable()
。例如,在 RxJS 源代码内部,它从不使用 asObservable()
,即使它经常使用和公开 Subjects => Observables。
有关更多信息,请参阅讨论:https://github.com/ReactiveX/rxjs/pull/2408
我想知道 BehaviorSubject
.
据我所知:
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().
但是下面的也让我很疑惑:
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().
这些定义有什么问题吗?
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)
}
}
以上哪种方法更好用,或者这两种方法的优缺点是什么?
更新:上次确定正确用法如下:
// 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 中推荐的做法是像这样进行类型转换:
private messageSubject = new BehaviorSubject<any>(undefined);
public messages$: Observable<any> = this.messageSubject;
这样,TypeScript 编译器将不允许您调用 next()
、error()
或 complete()
。只有在纯 JavaScript 中使用 RxJS 时才推荐使用 asObservable()
。例如,在 RxJS 源代码内部,它从不使用 asObservable()
,即使它经常使用和公开 Subjects => Observables。
有关更多信息,请参阅讨论:https://github.com/ReactiveX/rxjs/pull/2408