Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 迭代ngrx8约化器_Arrays_Angular_Redux_Ngrx_Ngrx Reducers - Fatal编程技术网

Arrays 迭代ngrx8约化器

Arrays 迭代ngrx8约化器,arrays,angular,redux,ngrx,ngrx-reducers,Arrays,Angular,Redux,Ngrx,Ngrx Reducers,接口: export interface IClient extends Array<IClient> { client_name: string } )) 影响: addClient = createEffect(() => { return this.actions.pipe( ofType(ClientActionTypes.addClient), switchMap(({ client }) => { retu

接口:

export interface IClient extends Array<IClient> {
  client_name: string
}
))

影响:

addClient = createEffect(() => {
    return this.actions.pipe(
      ofType(ClientActionTypes.addClient),
      switchMap(({ client }) => {
        return this.clientService.addClient(client).pipe(
          map((res) => ClientActionTypes.addClientSuccess(res)),
          catchError(error => {
            return of(ClientActionTypes.addClientFailure({ error }))
          })
        );
      })
    );
  });
在上面的代码中,我试图将数组附加到client_name中,但是我输入的值在数组中被分成多个值,我已经附加了redux存储,说明了它是如何被迭代的

例如,如果我输入了test和testing,它应该打印一个['test',“testing”]

您做错了

  • 您的界面不应扩展
    数组
    。这是一个简单的字符串

    导出接口IClient{
    客户端名称:字符串;
    }

  • 您的州应该包含有意义的名称:
    客户端名称**s**:IClient[]

    导出接口ClientState{
    客户名称:IClient[]
    }

    export const clientInitialState:ClientState={
    客户名称:[]
    }

  • 你的减速机应该是 A.配置正确,具有类型安全性。 B您的客户端名称数组应包含在添加新客户端之前初始化的以前的客户端。使用扩展运算符

    export const clientReducer=createReducer(
    客户初始状态,
    on(ClientActionTypes.addClientSuccess,(状态,操作)=>({
    ……国家,
    客户端名称:[…state.client\u名称,action.client\u名称]
    })

  • 还应正确配置您的效果:

    addClient$=createEffect(()=>{
    返回此.actions.pipe(
    类型(ClientActionTypes.addClient),
    开关映射((操作)=>{
    返回此.clientService.addClient(action.client\u name).pipe(
    map((res)=>ClientActionTypes.addClientSuccess({client_name:res})),
    catchError(错误=>{
    返回(ClientActionTypes.addClientFailure({error}))
    })
    );
    })
    );
    });

  • 我还没有测试过这段代码,所以请使用intellisense修复任何错误类型

    • 抱歉,使用了
      code
      而不是代码块格式,因此4个空格由于某些原因无法正常工作

    为什么要扩展Iclient接口?如果我不使用它,就会出现类似“Type必须有一个返回迭代器的'[Symbol.iterator]()'方法”这样的错误在您的界面中,您定义了字符串并将字符串分解为数组,这就是为什么您要获得字母数组Hanks,您能告诉我如何更新界面以使其按预期工作吗。客户端名称是字符串数组还是单个名称数组?非常感谢您的努力,我已经按照urs的确切代码进行了操作,我得到了如下错误“ERROR TypeError:state.client_names not iterable”仔细检查您的状态模型谢谢。是的,我得到的效果响应是字符串正确的。应该是状态问题。您从服务得到的响应是什么?
    export interface ClientState {
        client_name: IClient[]
    }
    
    export const clientInitialState: ClientState = {
        client_name: []
    }
    
    export const clientReducer = createReducer(
    clientInitialState,
    on(ClientActionTypes.addClientSuccess,(state, {client_name}) => ({
      ...state,
      client_name: [...client_name]
    })
    
    addClient = createEffect(() => {
        return this.actions.pipe(
          ofType(ClientActionTypes.addClient),
          switchMap(({ client }) => {
            return this.clientService.addClient(client).pipe(
              map((res) => ClientActionTypes.addClientSuccess(res)),
              catchError(error => {
                return of(ClientActionTypes.addClientFailure({ error }))
              })
            );
          })
        );
      });