gpt4 book ai didi

javascript - ngrx 8影响其他类型的调度 Action

转载 作者:行者123 更新时间:2023-12-01 12:05:38 28 4
gpt4 key购买 nike

我在这里与您联系有关ngrx 效果的信息,我要做的是拥有一个功能登录,该功能登录分配一个 Action 类型的登录,并且对该 Action 的影响将使用我的服务进行api 调用。
在这个返回 token 之后,我想分派(dispatch)另外两个 Action ,一个是 getUserMenu 类型,一个是 getUserInfo 类型。这两个 Action 的类型不同,效果也不同。

最后我有3家商店:
一个用于 token 和身份验证
一个用于用户信息
一个用于菜单信息

我试过这样的事情:

login = createEffect(
() =>
this.actions$
.pipe(
ofType(authLogin),
tap(action => {
console.log("EFFECT LOGINNNN");
return this.authService.postLogin(action.username,
action.password).pipe(
map((data: any) => {
console.log("AUTHTHHTHTH DATATA ", data);
let props = data.token;
let payload = {
token: data.token,
isAuthenticated: true
}
this.store.dispatch(moMenuHttpGetListAction({US_ID: action.username}));
this.store.dispatch(userHttpGetInfoAction({US_ID:action.username}));
this.localStorageService.setItem(AUTH_KEY, payload);
}))
})
),
{ dispatch: true }
);

如果我设置调度错误登录工作但没有调用获取用户信息和用户菜单的方法
但是当我设置 dispatch true 我有同样效果的无限循环
Action moMenuHttpGetListAction
看起来像这样:
moMenuHttpGetListEffect = createEffect(
() => this.actions$.pipe(
ofType(moMenuHttpGetListAction),
switchMap(action => {
console.log("MOMENU LOGINNNN");

return this.moMenuService.getKmApplications(action.US_ID).pipe(
map((data: any) => {
console.log("MOMENU DATATA ", data);
console.log("MOMENU DATATA ", action.US_ID);
let payload = {
MO_MENU: data
}

this.store.dispatch(moMenuSetListAction({payload: data}));
this.localStorageService.setItem(MENU_KEY, payload);
}))
})
),
{ dispatch: false }
);

但是在这个上,当我将 dispatch 设置为 true 时,我得到了编译错误。
我的 Action 看起来像:
import { createAction } from "@ngrx/store";
import { props } from "@ngrx/store";
import { MoMenu, MoMenuState } from "./mo_menu.models";

//TODO CHANGER ME_ID en US_ID
export const moMenuGetErrorAction = createAction("[User] Get Info");
export const moMenuGetIsLoadingAction = createAction("[User] Get Info");

export const moMenuSetErrorAction = createAction('[MoMenu] HTTP GET ACTION',
props<{error: string}>()
);
export const moMenuSetLoadingAction = createAction('[MoMenu] HTTP GET ACTION',
props<{loading: boolean}>()
);
export const moMenuHttpGetListAction = createAction('[MoMenu] HTTP GETLIST ACTION',
props<{US_ID: string}>()
);
export const moMenuHttpGetListErrorAction = createAction('[MoMenu] HTTP GET ACTION Error',
props<{error: any}>()
);

export const moMenuGetListAction = createAction("[MoMenu] Get List");
export const moMenuSetListAction = createAction("[MoMenu] Set Mo Menu List",
props<{payload: MoMenu[]}>());
export const moMenuDeleteAction = createAction("[MoMenu] Delete List");

这些是有人要求我补充的两个 reducer :

Ngrx 对我来说有点新,所以我很想在这方面提供一些帮助 =)
import { AuthState } from './auth.models';
import { authLogin,
authLogout ,
authGetErrorAction,
authGetIsLoadingAction,
authSetErrorAction,
authSetIsLoadingAction
} from './auth.actions';
import { createReducer, on, Action } from '@ngrx/store';

export const initialState: AuthState = {
isAuthenticated: false,
token: undefined,
isLoading: false,
HttpResponse: undefined
};

const reducer = createReducer(
initialState,
on(authSetErrorAction, (state, { error }) => ({
...state, HttpResponse: error
})),
on(authSetIsLoadingAction, (state, { isLoading }) => ({
...state, isLoading: isLoading
})),

on(authLogin, state => ({ ...state, isAuthenticated: true })),
on(authLogout, state => ({ ...state, isAuthenticated: false }))
);

export function authReducer(
state: AuthState | undefined,
action: Action
): AuthState {
return reducer(state, action);
}

import { MoMenuState } from "./mo_menu.models";
import {
moMenuGetListAction,
moMenuDeleteAction,
moMenuHttpGetListAction,
moMenuSetListAction,
moMenuHttpGetListErrorAction,

moMenuGetErrorAction,
moMenuGetIsLoadingAction,
moMenuSetErrorAction,
moMenuSetLoadingAction
} from "./mo_menu.actions";
import { createReducer, on, Action } from "@ngrx/store";

export const initialState: MoMenuState = {
isLoading: false,
HttpResponse: undefined,
MoMenuItems: null
}

const reducer = createReducer(
initialState,
on(moMenuSetErrorAction, (state, { error }) => ({
...state, HttpResponse: error
})),
on(moMenuSetLoadingAction, (state, { loading }) => ({
...state, isLoading: loading
})),

on(moMenuHttpGetListErrorAction, (state, { error }) => (
undefined)),
on(moMenuSetListAction, (state, { payload }) => ({
...state, MoMenus: payload
})),

on(moMenuHttpGetListAction, (state, { US_ID }) => ({
...state
})),
on(moMenuGetListAction, state => state),
on(moMenuDeleteAction, state => state)
);

export function moMenuReducer(
state: MoMenuState | undefined,
action: Action
): MoMenuState {
return reducer(state, action);
}

如果你们中的一些人对我有任何想法?

最佳答案

由于 NgRx 8 中的另一个操作,这就是我调度多个操作的方式:

@Injectable()
export class MyEffects {
myAction$ = createEffect(() =>
this.actions$.pipe(
ofType(myActions.firstAction),
switchMap(action => this.someService.getById(action.someId)),
switchMap((sth: Something) => [
myActions.firstActionSuccess({ payload: sth }),
myActions.secondAction({ payload: sth.xyz }),
// ... more actions
])
)
);

constructor(
private someService: SomeService,
private actions$: Actions
) { }
}

Action 如下所示:
export const firstAction = createAction('First Action', props<{ someId: number }>());
export const firstActionSuccess = createAction('First Action Success', props<{ payload: Something }>());
export const secondAction = createAction('Second Action', props<{ payload: string }>());

希望有帮助!

关于javascript - ngrx 8影响其他类型的调度 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57471082/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com