Angular 解析器出现 Ngrx 数据服务错误

Angular resolver with Ngrx data service error

我有一个正在工作的NGRX Data entity service,我想在访问路由之前预加载数据,因此我做了一个解析器。

import { Injectable } from "@angular/core";
import {
  ActivatedRouteSnapshot,
  Resolve,
  RouterStateSnapshot,
} from "@angular/router";
import { Observable, race } from "rxjs";
import { filter, first, tap, mapTo } from "rxjs/operators";
import { ExerciseEntityService } from "./exercise-entity.service";

@Injectable()
export class ExercisesResolver implements Resolve<boolean> {
  constructor(private exercisesService: ExerciseEntityService) {}

  resolve(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean> {
    return this.exercisesService.loaded$.pipe(
      tap((loaded) => {
        if (!loaded) {
          this.exercisesService.getAll();
        }
      }),
      filter((loaded) => !!loaded),
      first()
    );
  }
}

如果服务成功响应,路由将得到解析,但如果存在错误解析器,则不会得到解析。如果服务 returns 出现错误,我该如何管理?我试过用这种方式进行比赛,但它不起作用:

import { Injectable } from '@angular/core';
import {
  ActivatedRouteSnapshot,
  Resolve,
  RouterStateSnapshot
} from '@angular/router';
import { Observable, race } from 'rxjs';
import { filter, first, tap, mapTo } from 'rxjs/operators';
import { ExerciseEntityService } from './exercise-entity.service';

@Injectable()
export class ExercisesResolver implements Resolve<boolean> {
  constructor(private exercisesService: ExerciseEntityService) {}

  resolve(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean> {
    return race(
      this.exercisesService.loaded$.pipe(
        tap((loaded) => {
          if (!loaded) {
            this.exercisesService.getAll();
          }
        }),
        filter((loaded) => !!loaded),
        first()
      ),
      this.exercisesService.errors$.pipe(mapTo(true))
    );
  }
}

在尝试使用 combineLatest、mergeMap、race 和其他运算符的不同方法后,以这种方式解决了:

import { Injectable } from '@angular/core';
import {
  ActivatedRouteSnapshot,
  Resolve,
  RouterStateSnapshot
} from '@angular/router';
import { Observable, Subscriber } from 'rxjs';
import { filter, first, tap } from 'rxjs/operators';
import { ExerciseEntityService } from './exercise-entity.service';

@Injectable()
export class ExercisesResolver implements Resolve<boolean> {
  constructor(private exercisesService: ExerciseEntityService) {}

  resolve(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean> {
    const loadExercises = (subscriber: Subscriber<boolean>) => {
      this.exercisesService.loading$
        .pipe(
          filter((loading) => !loading),
          first()
        )
        .subscribe({
          next: (loading) => {
            subscriber.next(true);
            subscriber.complete();
          }
        });
      this.exercisesService.getAll();
    };

    const resolve$ = new Observable<boolean>((subscriber) => {
      this.exercisesService.loaded$
        .pipe(
          tap((entitiesLoaded) => {
            if (!entitiesLoaded) {
              loadExercises(subscriber);
            } else {
              subscriber.next(true);
              subscriber.complete();
            }
          })
        )
        .subscribe();
    });

    return resolve$;
  }
}