【问题标题】:Type 'Observable<void | Observable<void>>' is not assignable to type 'Observable<Action>'输入'可观察的<void | Observable<void>>' 不可分配给类型 'Observable<Action>'
【发布时间】:2018-06-12 19:39:07
【问题描述】:

我试图在NgRX 6 中理解effects

我有一个示例效果:

  @Effect()
  createNonce$: Observable<Action> = this.actions$.pipe(
    ofType(INVALID_SESSION),
    map(() => generateNonce(32)),
    map(nonce => of(this.store.dispatch(new IdentityRedirect(nonce)))),
    catchError(error => of(console.error(error)))
  );

我想要实现的是

  1. 收听INVALID_SESSION
  2. 调用generateNonce并返回结果
  3. 派发上一张地图的结果

我收到以下错误

[ts]
Type 'Observable<void | Observable<void>>' is not assignable to type 'Observable<Action>'.
  Type 'void | Observable<void>' is not assignable to type 'Action'.
    Type 'void' is not assignable to type 'Action'.

【问题讨论】:

    标签: angular rxjs ngrx


    【解决方案1】:
      @Effect()
      createNonce$: Observable<Action> = this.actions$.pipe(
        ofType(INVALID_SESSION),
        mergeMap(() =>
          of(generateNonce(32)).pipe(
            map(nonce => ({ type: REDIRECT_TO_LOGIN, payload: nonce }))
          )
        ),
        catchError(error => of({ type: REDIRECT_TO_LOGIN_FAILURE, payload: error }))
      );
    

    【讨论】:

      猜你喜欢
      • 2019-12-25
      • 1970-01-01
      • 2020-04-17
      • 2021-09-23
      • 2020-11-12
      • 2019-04-22
      • 2017-12-18
      • 2021-08-17
      • 2019-01-10
      相关资源
      最近更新 更多