Ngrx mergeMap inside catchError加入更多动作让我出错

Ngrx mergeMap inside catchError to join more actions get me error

我想分派两个动作 在 catchError 块内 但是我遇到了这个错误

Argument of type '(error: {}) => void' is not assignable to parameter of type '(value: {}, index: number) => ObservableInput<{}>'. Type 'void' is not assignable to type 'ObservableInput<{}>'

import { Injectable, Inject } from '@angular/core';
import { Actions, Effect } from '@ngrx/effects';
import { Router } from '@angular/router';

import { catchError, map, mergeMap, switchMap } from 'rxjs/operators';
import { of } from 'rxjs/observable/of';

import {
  AuthenticationAccount
} from '../../models/account.model';

import * as fromActions from '../actions';

import { ACCOUNT_SERVICE } from '../../tokens';

@Injectable()
export class AccountEffects {
  @Effect()
  account$ = this.actions$.ofType(fromActions.ACCOUNT)
    .pipe(
    switchMap(() => {
      return this.accountService.account()
        .pipe(
        map((account: AuthenticationAccount) => {
          return { type: fromActions.ACCOUNT_SUCCESS, payload: account };
        }),
        catchError((error) => {
          this.router.navigateByUrl('/home');
          return mergeMap((error) => {
            [{ type: fromActions.ACCOUNT_FAIL, payload: error },
            { type: fromActions.LOGIN_RESET}]
          })
        })
        )
    })
    );
  constructor(
    private actions$: Actions,
    private router: Router,
    @Inject(ACCOUNT_SERVICE) private accountService
  ) { }
}

你能帮帮我吗?

catchError 部分错误,您将 mergeMap 用作静态方法,但在 pipe 范围之外,这是不可能的。

修改如下:

import { from } from 'rxjs/observable/from':

catchError((error) => {
          this.router.navigateByUrl('/home');
          const actions = [{ type: fromActions.ACCOUNT_FAIL, payload: error }, { type: fromActions.LOGIN_RESET}];
          return from(actions);
        })

顺便说一句:

  • 为什么要使用令牌注入服务?
  • 为什么不使用动作创建器?您可以使用 类 或方法来生成新对象,而不是创建新对象。