Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading