我正在尝试将对象序列化为查询参数。在AngularJS
我使用$httpParamSerializer
。
Angular 5
中有哪些内容?
答案 0 :(得分:4)
以下是我对你问题的看法。这使用HttpParamsOptions
和HttpParams
。 HttpParamsOptions
将通过在fromObject
属性中传递一个对象来构建params。您可以在注释中看到进行API调用时的样子。
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { HttpParamsOptions } from '@angular/common/http/src/params';
@Injectable()
export class ApiService {
constructor(private httpClient: HttpClient) { }
getThingsWithParams(): any { // you could pass your object in as a function parameter
const myObject: any = { this: 'thisThing', that: 'thatThing', other: 'otherThing'};
const httpParams: HttpParamsOptions = { fromObject: myObject } as HttpParamsOptions;
const options = { params: new HttpParams(httpParams) };
return this.httpClient.get<any>('http://localhost:34479/api/products/168', options);
// This is what is sent to the API:
// http://localhost:34479/api/products/168?this=thisThing&that=thatThing&other=otherThing
}
}
我认为这就是你所追求的。一种根据对象的属性构建参数的方法。