我想派遣两个动作在捕距块内但是我有这个错误
类型的参数'(错误:{})=> 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:
- 为什么要使用令牌注入服务?
- 为什么不使用动作创造者?而不是创建新对象,您可以使用类或方法来生成这些对象。