AngularJS中是否有类似$HttpParamSeralizer的东西可用?
我正在尝试序列化对象以查询参数。在AngularJS中是否有类似$HttpParamSeralizer的东西可用?,angularjs,angular,Angularjs,Angular,我正在尝试序列化对象以查询参数。在AngularJS中,我使用$httpParamSerializer 《Angular 5》中提供了什么?以下是我对您的问题的看法。这将使用HttpParamsOptions和HttpParams。HttpParamsOptions将通过在fromObject属性中传递一个对象来构建参数。您可以在注释中看到API调用时的样子 import { Injectable } from '@angular/core'; import { HttpClient, Http
AngularJS
中,我使用$httpParamSerializer
《Angular 5》中提供了什么?以下是我对您的问题的看法。这将使用
HttpParamsOptions
和HttpParams
。HttpParamsOptions
将通过在fromObject
属性中传递一个对象来构建参数。您可以在注释中看到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
}
}
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpClient,HttpParams};
从“@angular/common/http/src/params”导入{HttpParamsOptions};
@可注射()
出口级服务{
构造函数(私有httpClient:httpClient){}
getThingsWithParams():可以将对象作为函数参数传入的任何{//
const myObject:any={这个:'这个东西',那个:'那个东西',其他:'其他东西';
常量httpParams:HttpParamsOptions={fromObject:myObject}作为HttpParamsOptions;
const options={params:new-HttpParams(HttpParams)};
返回此.httpClient.get这是我对您的问题的看法。它使用HttpParamsOptions
和HttpParams
。HttpParamsOptions
将通过在fromObject
属性中向其传递一个对象来构建参数。您可以在注释中看到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
}
}
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpClient,HttpParams};
从“@angular/common/http/src/params”导入{HttpParamsOptions};
@可注射()
出口级服务{
构造函数(私有httpClient:httpClient){}
getThingsWithParams():可以将对象作为函数参数传入的任何{//
const myObject:any={这个:'这个东西',那个:'那个东西',其他:'其他东西';
常量httpParams:HttpParamsOptions={fromObject:myObject}作为HttpParamsOptions;
const options={params:new-HttpParams(HttpParams)};
返回此.httpClient.get是的,通过查看httparms的源代码使其正常工作,需要“fromObject”。感谢您的努力。答案已接受。是的,通过查看httparms的源代码使其正常工作,需要“fromObject”。感谢您的努力。答案已接受。