'Angular combinelatest using subject without initial value
I am using Angular RXJS, I have multiple Behavior Subjects, My problem is: I want the posts$ stream to emit initially without setting a value for the category filter when loading the page, but I don't want to set a default value for the CategorySubject!, I tried NULL its no working and If I change it to Subject;combinelatest will not work initially, how can I achieve this?
Thanks in advance.
private CategorySubject = new BehaviorSubject<number>();
//CategorySubject = new Subject<number>();
//CategorySubject = new BehaviorSubject<number>(null);
public CategoryAction$ = this.CategorySubject.asObservable();
//pageIndex Subject
private pageIndexSubject = new BehaviorSubject<number>(1);
public pageIndexAction$ = this.pageIndexSubject.asObservable();
//Search/filter Subject
private searchSubject = new BehaviorSubject<string>('');
public searchAction$ = this.searchSubject.asObservable();
Posts$ = combineLatest([
this.pageIndexAction$,
this.searchAction$
this.CategoryAction$
]).pipe()
Solution 1:[1]
When you have strictNullChecks
enabled, you could either specify your BehaviorSubject's type as BehaviorSubject<number|null>
or you could use a plain Subject<number>
and use startWith
to emit your initial null
value inside the combineLatest
:
private CategoryAction$ = new Subject<number>();
...
Posts$ = combineLatest([
this.pageIndexAction$,
this.searchAction$
this.CategoryAction$.pipe(startWith(null))
]).pipe()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | BizzyBob |