首页 > 解决方案 > 为什么 Angular 解析器在使用 of() 返回可观察对象时不返回任何数据,但在使用 of().pipe(delay(1000)) 时返回

问题描述

我的问题就像标题一样。我不完全理解为什么 Angular 解析器在使用创建返回的对象时不返回(不解析)任何数据,of([...someData])但在使用时工作of([...someData]).pipe(delay(1000)

举一个简单的例子:解析器:

 resolve(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ):
    | Schedule.FetchAllSportTypesSuccess
    | Schedule.FetchAllSportTypesFailed
    | Observable<Schedule.FetchAllSportTypesSuccess | Schedule.FetchAllSportTypesFailed>
    | Promise<Schedule.FetchAllSportTypesSuccess | Schedule.FetchAllSportTypesFailed> 
    {

    this.store.dispatch(new Schedule.FetchAllSportTypes());

    const responseOK: Observable<Schedule.FetchAllSportTypesSuccess> = this.actions$.pipe(ofAction(Schedule.FetchAllSportTypesSuccess));

    const responseError = this.actions$.pipe(
      ofAction(Schedule.FetchAllSportTypesFailed),
      tap(() => this.router.navigate(['']))
    );

    return race(responseOK, responseError).pipe(first());
  }

this.store.dispatch(new Schedule.FetchAllSportTypes())调用fetchAllSportTypes方法:

fetchAllSportTypes(): Observable<SportType[]>{

}

当实现fetchAllSportTypes(): Observable<SportType[]>如下时,解析器不返回任何数据,它只是一直等待:

fetchAllSportTypes(): Observable<SportType[]>{
  return of([...someData])
}

但是当实现如下时,解析器确实按预期工作:

fetchAllSportTypes(): Observable<SportType[]>{
      return of([...someData]).pipe(delay(1000))
    }

标签: angularrxjsngxs

解决方案


推荐阅读