作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发 Angular 10 版本的 ngrx
在 Reducer 中,想添加将删除用户的操作
当我尝试下面的代码时,遇到了问题:
错误类型错误:无法删除 [object Array] 的属性“1”
不理解这种行为,因为我只是试图通过其位置从数组中删除一个元素
我调试并看到它正在获取数组中特定对象的索引state.users.splice(indx, 1);
export interface State {
users: IProduct[],
error: string
}
export const initialState: State = {
users: [],
error: ''
};
export function reducer(state = initialState, action: UserActions): State {
switch (action.type) {
case UserActionTypes.DeleteUser:
const indx = state.users.findIndex(user => user.id === action.payload.data.id);
state.users.splice(indx, 1);
return {
...state
}
case UserActionTypes.LoadUsers:
return {
...state
}
case UserActionTypes.LoadUsersSuccess:
return {
...state,
users: [...state.users, ...action.payload.data],
error: ''
}
case UserActionTypes.LoadUsersFailure:
return {
...state,
users: [],
error: action.payload.error
}
default:
return state;
}
}
最佳答案
终于找到答案了
我在新变量 newState 中分配了数据,然后从那个
case UserActionTypes.DeleteUser:
const index = state.users.findIndex(user => user.id === action.payload.data.id);
let newState = [...state.users];
newState.splice(index, 1);
return {
users: newState,
error:''
}
export interface State {
users: IProduct[],
error: string
}
export const initialState: State = {
users: [],
error: ''
};
export function reducer(state = initialState, action: UserActions): State {
switch (action.type) {
case UserActionTypes.DeleteUser:
const index = state.users.findIndex(user => user.id === action.payload.data.id);
let newState = [...state.users];
newState.splice(index, 1);
return {
users: newState,
error:''
}
case UserActionTypes.LoadUsers:
return {
...state
}
case UserActionTypes.LoadUsersSuccess:
return {
...state,
users: [...state.users, ...action.payload.data],
error: ''
}
case UserActionTypes.LoadUsersFailure:
return {
...state,
users: [],
error: action.payload.error
}
default:
return state;
}
}
关于javascript - 错误类型错误 : Cannot delete property '1' of [object Array],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63753009/
我是一名优秀的程序员,十分优秀!