angular 4.0.2,ngrx/store,减速器
angular 4.0.2, ngrx/store, reducers
我在 ngrx git 版本 4.0.x 存储库中的示例应用程序的帮助下,使用 ngrx/store 4.0.2 制作了一个应用程序。我用过的其他模块 运行 这部分应用程序是。
@angular/cli:1.3.0
节点:v8.1.3
npm:5.3.0。
我还有其他遵循相同模式的模块,每个人都出现了同样的问题。请帮我解决这个问题。
我调用 api 的效果函数是 .
@Effect() loadUsers$: Observable<Action> = this.actions$
.ofType(ObjectActions.LOAD_USERS)
.startWith(new ObjectActions.Loadusers())
.switchMap(() =>
this.service.loadUsers()
.map((users: UserModel[]) => new ObjectActions.Loaduserssuccess(users))
.catch(err => of(new ObjectActions.Loadusersfailure(err)))
)
我的减速机是users.reducer.ts
export interface UserState {
user_ids: string[]
users: {[id: string]: Array<UserModel>}
selectedUserId: string| null;
}
const initialState: UserState = {
user_ids: [],
users: {},
selectedUserId: null
}
export function UsersReducer(state = initialState, action: UserActions.Actions): UserState {
switch(action.type){
case UserActions.LOAD_USERS_SUCCESS:
return {
user_ids: action.payload.user_ids,
users: action.payload.users,
selectedUserId: null
}
case UserActions.LOAD_USERS_FAILURE:
case UserActions.ADD_USER_SUCCESS:
return {
user_ids: [ ...state.user_ids, action.payload.user_id],
users: Object.assign({}, state.users, { [action.payload.user_id]: action.payload}),
selectedUserId: state.selectedUserId
};
case UserActions.ADD_USER_FAILURE:
case UserActions.GET_USER_SUCCESS:
case UserActions.SELECT_USER_SUCCESS:
return {
user_ids: state.user_ids,
users: state.users,
selectedUserId: action.payload,
}
case UserActions.DELETE_USER_SUCCESS:
enter code here
default:
return state
}
}
export const getUsersId= (state: UserState) => state.user_ids
export const getUsers = (state: UserState) => state.users
export const getAllUsers = createSelector(getUsers, getUsersId, (users, user_ids) => {
return user_ids.map(user_id => users[user_id]) || new Map();
});
export const selectedUserId = (state: UserState) => state.selectedUserId;
export const getSelectedUser = createSelector(getUsers, selectedUserId, (entities, selectedId) => {
return entities[selectedId];
});
export interface AppState {
users: fromUser.UserState;
loggedinuser: fromLogin.LoginState;
}
export const reducer: ActionReducerMap<AppState> = {
users: fromUser.UsersReducer,
loggedinuser: fromLogin.LoginReducer,
};
export const getUserAppState = createFeatureSelector<AppState>('users');
export const getUserState = createSelector(
getUserAppState,
(state: AppState) => state.users
);
export const getUsers = createSelector(getUserState, fromUser.getAllUsers)
从商店中选择 getUsers 时,出现此错误
UsersComponent.html:50 ERROR TypeError: Cannot read property 'map' of undefined
at users.reducer.ts:74
at index.js:76
at index.js:36
at index.js:90
at index.js:36
at store.es5.js:698
at memoized (store.es5.js:666)
at store.es5.js:702
at MapSubscriber.memoized [as project] (store.es5.js:666)
at MapSubscriber.webpackJsonp.../../../../rxjs/operator/map.js.MapSubscriber._next (map.js:77)
这是因为选择器试图循环一些未定义的东西。它主要发生在 API 调用之前商店为空但您的选择器试图循环遍历它的情况下。因为我们仅在 API 响应时才保存数据。
我在 ngrx git 版本 4.0.x 存储库中的示例应用程序的帮助下,使用 ngrx/store 4.0.2 制作了一个应用程序。我用过的其他模块 运行 这部分应用程序是。 @angular/cli:1.3.0 节点:v8.1.3 npm:5.3.0。 我还有其他遵循相同模式的模块,每个人都出现了同样的问题。请帮我解决这个问题。
我调用 api 的效果函数是 .
@Effect() loadUsers$: Observable<Action> = this.actions$
.ofType(ObjectActions.LOAD_USERS)
.startWith(new ObjectActions.Loadusers())
.switchMap(() =>
this.service.loadUsers()
.map((users: UserModel[]) => new ObjectActions.Loaduserssuccess(users))
.catch(err => of(new ObjectActions.Loadusersfailure(err)))
)
我的减速机是users.reducer.ts
export interface UserState {
user_ids: string[]
users: {[id: string]: Array<UserModel>}
selectedUserId: string| null;
}
const initialState: UserState = {
user_ids: [],
users: {},
selectedUserId: null
}
export function UsersReducer(state = initialState, action: UserActions.Actions): UserState {
switch(action.type){
case UserActions.LOAD_USERS_SUCCESS:
return {
user_ids: action.payload.user_ids,
users: action.payload.users,
selectedUserId: null
}
case UserActions.LOAD_USERS_FAILURE:
case UserActions.ADD_USER_SUCCESS:
return {
user_ids: [ ...state.user_ids, action.payload.user_id],
users: Object.assign({}, state.users, { [action.payload.user_id]: action.payload}),
selectedUserId: state.selectedUserId
};
case UserActions.ADD_USER_FAILURE:
case UserActions.GET_USER_SUCCESS:
case UserActions.SELECT_USER_SUCCESS:
return {
user_ids: state.user_ids,
users: state.users,
selectedUserId: action.payload,
}
case UserActions.DELETE_USER_SUCCESS:
enter code here
default:
return state
}
}
export const getUsersId= (state: UserState) => state.user_ids
export const getUsers = (state: UserState) => state.users
export const getAllUsers = createSelector(getUsers, getUsersId, (users, user_ids) => {
return user_ids.map(user_id => users[user_id]) || new Map();
});
export const selectedUserId = (state: UserState) => state.selectedUserId;
export const getSelectedUser = createSelector(getUsers, selectedUserId, (entities, selectedId) => {
return entities[selectedId];
});
export interface AppState {
users: fromUser.UserState;
loggedinuser: fromLogin.LoginState;
}
export const reducer: ActionReducerMap<AppState> = {
users: fromUser.UsersReducer,
loggedinuser: fromLogin.LoginReducer,
};
export const getUserAppState = createFeatureSelector<AppState>('users');
export const getUserState = createSelector(
getUserAppState,
(state: AppState) => state.users
);
export const getUsers = createSelector(getUserState, fromUser.getAllUsers)
从商店中选择 getUsers 时,出现此错误
UsersComponent.html:50 ERROR TypeError: Cannot read property 'map' of undefined
at users.reducer.ts:74
at index.js:76
at index.js:36
at index.js:90
at index.js:36
at store.es5.js:698
at memoized (store.es5.js:666)
at store.es5.js:702
at MapSubscriber.memoized [as project] (store.es5.js:666)
at MapSubscriber.webpackJsonp.../../../../rxjs/operator/map.js.MapSubscriber._next (map.js:77)
这是因为选择器试图循环一些未定义的东西。它主要发生在 API 调用之前商店为空但您的选择器试图循环遍历它的情况下。因为我们仅在 API 响应时才保存数据。