无法复制返回的数组类型,类型'必须具有返回迭代器的{[Symbol.iterator]()'方法

时间:2019-09-21 14:35:15

标签: javascript angular typescript redux ngrx

接口:

export interface IClient {
  client_name: string
  client_id?: string
}

减速器

export interface ClientState {
    client_name: IClient[]
}

export const clientInitialState: ClientState = {
    client_name: null
}


export const clientReducer = createReducer(
    clientInitialState,
    on(ClientActionTypes.addClientSuccess,(state, {client_name}) => {
      const item = [...client_name]
      return {
        ...state,
        newItem

      }
    }
  ))

使用上面的代码,我试图在[[Item1“,” Item2“]之类的数组内附加值,但出现错误“ Type”必须具有返回迭代器的'Symbol.iterator'方法。该错误来自[... client_name]传播运算符。我需要修复此问题或其他任何方式的帮助。谢谢

enter image description here

2 个答案:

答案 0 :(得分:1)

这是因为"Content-Type" => "application/x-www-form-urlencoded"的初始值为clientInitialState.client_name
尝试传播null将给您该错误:

null

您可以使用const foo = null; const bar = [...foo]; // object null is not iterable (cannot read property Symbol(Symbol.iterator)) 初始化变量:

[]

答案 1 :(得分:1)

您的代码应为

export const clientReducer = createReducer(
    clientInitialState,
    on(ClientActionTypes.addClientSuccess,(state, {client_name}) => {
      return {
        ...state,
        newItem: [...state.client_name, ...client_name]
      }
    }
  ))