我想在catchError块中调度两个动作,但是我遇到了这个错误
类型'(error:{})=> void'的参数不能分配给类型'(value:{},index:number)=> ObservableInput <{}>'的参数。类型'void'不能分配给'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);
})
BTW: