【问题标题】:How to send interface as a parameter to HTTP get request in angular services如何将接口作为参数发送到角度服务中的 HTTP 获取请求
【发布时间】:2021-01-16 20:03:29
【问题描述】:

难以将接口作为HTTP get请求的参数传递

sample.service.ts

 export interface Product{
    Id:any;
    product : any;
   }

public getProduct(item:Product): Observable<any> {
    const base = this.http.get('/api/getproducts', item);
    const request = base.pipe(
      map((data: TokenResponse) => {
        console.log('display data==>', data);
          return data;
      })
    );
    return request;
  }

sample.component.ts

 this.ps.getProduct(this.product).subscribe(
      data => {
               console.log("results",data.results)
             });

提前致谢

【问题讨论】:

    标签: angular service http-get


    【解决方案1】:

    使用HttpParams 传递您的参数,如下所示:

    const headers = new HttpHeaders().append('header', 'value');
    const params = new HttpParams().append('Id', item.ID);
    const params = new HttpParams().append('product', item.product);
    this.http.get('url', {headers, params});
    

    【讨论】:

    • 感谢@Ravinder,.append(),dint 附加值,所以我使用了 .set(),它现在可以正常工作了。
    猜你喜欢
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 2020-09-27
    • 2016-11-08
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 2013-12-09
    相关资源
    最近更新 更多