Angular 5:AngularJS中是否有类似$ httpParamSerailizer的东西? [英] Angular 5: Is there anything like $httpParamSerailizer that was available in AngularJS?

查看:54
本文介绍了Angular 5:AngularJS中是否有类似$ httpParamSerailizer的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试序列化对象以查询参数.在 AngularJS 中,我使用 $ httpParamSerializer .

I am trying to serialize object to query parameters. In AngularJS I use $httpParamSerializer.

Angular 5 中有哪些可用功能?

推荐答案

这是我对您的问题的看法.这使用 HttpParamsOptions HttpParams . HttpParamsOptions 将通过在 fromObject 属性中向其传递一个对象来构建参数.您可以在注释中看到进行API调用时的外观.

Here is my take on your question. This uses the HttpParamsOptions and HttpParams. The HttpParamsOptions will build out the params by passing it an object in the fromObject property. You can see in the comment what that looks like when the API call is made.

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
  }
}

我认为这就是你所追求的.一种基于对象属性构建参数的方法.

I think this is what you are after. A way to have the parameters build based on the properties of an object.

https://github.com/angular/angular/blob/530b824faa29460a87c2401bc61d22a7fb56f939/packages/common/http/src/params.ts#L76

这篇关于Angular 5:AngularJS中是否有类似$ httpParamSerailizer的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆