首页 > 解决方案 > 以角度将字符串列表作为查询参数传递给http.get

问题描述

我的 API 中有一个 get 方法,它接收路径上的 ID 和日期列表作为查询参数,我的 http 服务检索到这个错误

public ObterSimulacao<T>(inscricao:string,listaDatas: HttpParams){
    return this.http.get<T>('http://localhost:50441/api/v1/Movimento/Simulacao' + inscricao ,{listaDatas});
}
ERROR in src/app/Data.service.ts(34,95): error TS2345: Argument of type '{ listaDatas: HttpParams; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
  Object literal may only specify known properties, and 'listaDatas' does not exist in type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.

我还尝试将 listaDatas 设置为:any 和:string[],得到同样的错误,有什么想法吗?

标签: angularhttp

解决方案


您需要删除您得到的请求中的括号:

public ObterSimulacao<T>(inscricao:string,listaDatas: HttpParams){
    return this.http.get<T>('http://localhost:50441/api/v1/Movimento/Simulacao' + inscricao , listaDatas);
}

括号中的所有参数都需要链接到一个键。例如,如果你想要一个标题,你可以这样做:

return this.http.get<T>('http://localhost:50441/api/v1/Movimento/Simulacao' + inscricao , listaDatas, {headers: yourHeader});

希望能帮助到你


推荐阅读