我正在使用Angular 8和NGRX8。我有一个动作:
export const loadEnvironment = createAction(
LicencingActionTypes.LoadEnvironment,
props<{environment: Environment}>()
);
及相应的效果:
loadEnvironment = createEffect(() => this.actions$.pipe(
ofType(LicencingActionTypes.LoadEnvironment),
exhaustMap((action) =>
this.authService.
getToken(LicencingEffects.getAuthDetailsForEnvironment(action.environment))
.pipe(
switchMap((token) => [
AuthActions.onLogin({token: token}),
LicencingActions.onLoadEnvironment({environment: action.environment})
]),
catchError(error => of(AuthActions.onLoginError({error: error})))
)
)
));
我一直在阅读有关NGRX 8(https://ngrx.io/guide/effects#incorporating-state)的文档。
他们的示例表明,您可以仅使用action属性,而无需强制转换动作类型:
...
exhaustMap(action =>
this.authService.login(action.credentials)
...
Webpack无法编译,并且出现以下错误:
ERROR in src/app/licencing/effects/licencing.effects.ts(20,69): error TS2339: Property 'environment' does not exist on type 'never'.
Screenshot of code with errors highlighted
我要去哪里错了?
答案 0 :(得分:0)
尝试如下更改您的effect
-
loadEnvironment = createEffect(() => this.actions$.pipe(
ofType(loadEnvironment), //<-- this is your action
exhaustMap(action => //<-- getting rid of extra parenthesis
this.authService.
getToken(LicencingEffects.getAuthDetailsForEnvironment(action.environment))
.pipe(
switchMap((token) => [
AuthActions.onLogin({token: token}),
LicencingActions.onLoadEnvironment({environment: action.environment})
]),
catchError(error => of(AuthActions.onLoginError({error: error})))
)
)
));
参考样本implementation
答案 1 :(得分:0)
动作必须在动作文件底部的类型联合中声明:
const all = union({
info,
appError
});
export type CoreActionsUnion = typeof all;
,然后在以该类型作为Actions的类型参数注入的Effects类构造函数中:
constructor(private actions$: Actions<CoreActionsUnion>) { }