When is asObservable()
needed on a Subject (e.g. BehaviorSubject) to get a observable of the subject? The subject isself can be casted to an Observable as well.
name1$
and name2$
?name1$
or name2$
)?import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs';
export class Person {
private nameSubject: BehaviorSubject<string> = new BehaviorSubject<string>('lorem');
public get name1$(): Observable<string> {
return this.nameSubject.asObservable();
}
public get name2$(): Observable<string> {
return this.nameSubject;
}
public setName(value: string): void {
this.nameSubject.next(value);
}
}
Thank You for your answers in advance!
In general, it's recommended to use asObservable()
only when using RxJS in JavaScript. With TypeScript you can use just type casting and it won't let you call next()
and so on.
private nameSubject: BehaviorSubject<string> = new BehaviorSubject<string>('lorem');
public nameSubject$: Observable<string> = this.nameSubject;
This is the officially recommended way of turning Subjects to Observables in TypeScript.
For details see https://github.com/ReactiveX/rxjs/pull/2408#issuecomment-282077506.
There is not much difference, with asObservable()
it mainly just block your access to observer methods - complete
, next
, error
to prevent you from accidentally calling them
const a = new Subject();
console.log(a.asObservable().next)
// undefined
it is better to return asObservable()
to be on the safe side if you not using that particular stream as observer
I wrote a gulp file that works but...not at any attempts. Indeed, I get this weird error 1 time / 3: [12:25:51] Starting 'default'... [12:25:51] Starting 'clean'... [12:25:51] Finished 'clean' ...
I wrote a gulp file that works but...not at any attempts. Indeed, I get this weird error 1 time / 3: [12:25:51] Starting 'default'... [12:25:51] Starting 'clean'... [12:25:51] Finished 'clean' ...
I need to scroll to a form field (a displayfield actually) automatically. Since the field is dynamic, sometimes it is already shown in the rendered area of page, sometimes it is not. How can I do ...
I need to scroll to a form field (a displayfield actually) automatically. Since the field is dynamic, sometimes it is already shown in the rendered area of page, sometimes it is not. How can I do ...
I have read similar questions and no solutions seem to be working for this. I have a Angular material input box. The input should receive only numbers and a decimal dot. All other characters should be ...
I have read similar questions and no solutions seem to be working for this. I have a Angular material input box. The input should receive only numbers and a decimal dot. All other characters should be ...
This is a clean version of the my situation: const person1 = { name: "Person1 Name", hairColor: "Brown", backpack: { color: "Army-Green", content: [ "item1", ...
This is a clean version of the my situation: const person1 = { name: "Person1 Name", hairColor: "Brown", backpack: { color: "Army-Green", content: [ "item1", ...