Angular combinelatest using subject 没有初始值

Angular combinelatest using subject without initial value

我正在使用 Angular RXJS,我有多个行为主题, 我的问题是:我希望 posts$ 流在加载页面时最初发出而不为类别过滤器设置值,但我不想为 CategorySubject 设置默认值!,我尝试 NULL 它不起作用,如果我将其更改为 Subject;combinelatest 最初无法使用,我该如何实现?

提前致谢。

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

启用 strictNullChecks 后,您可以将 BehaviorSubject 的类型指定为 BehaviorSubject<number|null> 或者您可以使用普通的 Subject<number> 并使用 startWith 发出您的初始null 内的值 combineLatest:

 private CategoryAction$ = new Subject<number>();
 
 ...
 
 Posts$ = combineLatest([
    this.pageIndexAction$,
    this.searchAction$
    this.CategoryAction$.pipe(startWith(null))
  ]).pipe()