尝试通过订阅使用 forkJoin

Trying to use forkJoin with subscription

我正在尝试在订阅上使用 forkJoin,由于某种原因它根本不起作用,我无法控制台记录我的代码,很可能甚至没有达到 .subscribe,我在这里缺少什么?

public SUB_getActSearch: Subscription;

this.SUB_getActSearch = this.searchService.getActSearch({ ...searchOBJ2, PageSize: this.pageSize, PageNumber: this.currentPage }).subscribe((res) => {
        this.Act_Data = [];
        let headerPagin = JSON.parse(res.headers.get('X-Pagination'));
        this.elementsLength = headerPagin.totalCount;
        this.currentPage = headerPagin.currentPage;
        this.pageSize = headerPagin.pageSize;
        res.body.map((param) => {
          this.Act_Data.push(param);
        });
        this.ActDataSource = new MatTableDataSource<Element>(this.Act_Data);
        // console.log('Your form data : ', this.ActDataSource);
      });

  public testingforkJoin(): void {
    forkJoin(
      this.SUB_getActSearch
    ).subscribe(val => console.log("xxxxx", val));
  }

我想在 this.SUB_getActSearch 上使用 forkJoin 并在 this.SUB_getActSearch 完成后立即在控制台记录一些内容,只是为了测试 forkJoin 运算符

检查您 运行 angular 所在的控制台,您应该在那里遇到一些错误。 forkJoin 不将订阅作为输入。

因为你只有一个 observable (this.searchService.getActSearch(...)),所以不需要 forkJoin。您对 observables 和订阅的理解似乎有点偏差,但我将对您想要完成的事情做出一些假设,并在此过程中发表一些评论。

public getActSearch$: Observable<any>; // specify correct type instead of any if possible

ngOnInit() {
    // I'm not sure if this code is inside an ngOnInit since you didn't 
    // specify where the code is in your example, but I'll assume it is. 
    this.getActSearch$ = this.searchService
        .getActSearch({ 
            ...searchOBJ2, 
            PageSize: this.pageSize, 
            PageNumber: this.currentPage 
        }) 
        // You will need to use shareReplay if the observable only emits one value 
        // and you want to be able to subscribe to it multiple times
        .pipe(shareReplay()); 

        // This will fire off the getActSearch request (which I assume is a http call)
        // And the initialize the values with the result
        this.getActSearch$.subscribe(res => this.initValues(res));
}

// I separated the observer code from your example into its own method
// so that you can choose when to initialize your values
private initValues(res) {
    this.Act_Data = [];
    let headerPagin = JSON.parse(res.headers.get('X-Pagination'));
    this.elementsLength = headerPagin.totalCount;
    this.currentPage = headerPagin.currentPage;
    this.pageSize = headerPagin.pageSize;
    res.body.map((param) => {
        this.Act_Data.push(param);
    });
    this.ActDataSource = new MatTableDataSource<Element>(this.Act_Data);
    // console.log('Your form data : ', this.ActDataSource);
});

public testingSubscribingAgain(): void {
  // Because the observable is replayed through `shareReplay`, when you come here you will fire a new http request. 
  // It's unclear if that's your intention.
  this.getActSearch$
      .subscribe(val => {
          // Do you want to reinitialize the values here?
          this.initValues(val); 
          console.log("xxxxx", val));
  })
}