我想发出两个动作 在catchError块中 但我有这个错误
类型的参数'(错误:{})=>空隙'不能分配给参数 类型'(值:{},索引:数字)=> ObservableInput< {}>&#39 ;.类型 '空隙'不能分配给' 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
) { }
}
请帮帮我吗?
答案 0 :(得分:3)
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);
})
顺便说一句: