[英]Property 'payload' does not exist on type 'never'
I use ngrx in my Angular app.我在我的 Angular 应用程序中使用 ngrx。
How to fix the error Property 'payload' does not exist on type 'never'.如何修复错误属性“payload”在类型“never”上不存在。
// auth.effects.ts // auth.effects.ts
effectLogInSuccess$ = createEffect(
() =>
this.actions$.pipe(
ofType(AuthActionTypes.LOGIN_SUCCESS),
tap((user) => {
console.log(user);
localStorage.setItem('token', user.payload.token);
this.router.navigateByUrl('/');
})
),
{ dispatch: false }
);
// auth.actions.ts // auth.actions.ts
export const actionLogInSuccess = createAction(
AuthActionTypes.LOGIN_SUCCESS,
props<{ payload: any }>()
);
Solution 1 : to pass authActions.actionLogInSuccess inside ofType operator解决方案 1 :在 ofType 运算符内部传递 authActions.actionLogInSuccess
// auth.effects.ts // auth.effects.ts
effectLogInSuccess$ = createEffect(
() =>
this.actions$.pipe(
ofType(authActions.actionLogInSuccess),
tap((action) => {
console.log(action);
localStorage.setItem('token', action.payload.token);
this.router.navigateByUrl('/');
})
),
{ dispatch: false }
);
Solution 2 : to provide a type for action parameter解决方案 2 :为操作参数提供类型
// auth.effects.ts // auth.effects.ts
effectLogInSuccess$ = createEffect(
() =>
this.actions$.pipe(
ofType(AuthActionTypes.LOGIN_SUCCESS),
tap((action: any) => {
console.log(action);
localStorage.setItem('token', action.payload.token);
this.router.navigateByUrl('/');
})
),
{ dispatch: false }
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.