我想在GET请求中发送查询参数.我的班级看起来像这样:
@Injectable() export class Loader implements TranslateLoader{ constructor(private http: Http){ } getTranslation(lang: string): Observable{ return this.http.get(routes.Localization.Get) ;// in that place I need to pass params } }
我怎样才能做到这一点?
你可以利用这个URLSearchParams
类:
getTranslation(lang: string): Observable{ let params = new URLSearchParams(); params.set('param1', 'value1'); return this.http.get(routes.Localization.Get, { search: params }); }
这将导致像这样的URL(参数查询字符串添加)http://...?param1=value1
.
请参阅此类的文档:
https://angular.io/docs/ts/latest/api/http/index/URLSearchParams-class.html
它现在提供对编码/解码参数的支持.
这非常简单 - 您可以定义URLSearchParams
并在方法的第二个参数中传递它们http.get
:
import { URLSearchParams } from '@angular/http' let params: URLSearchParams = new URLSearchParams(); params.set('param1', 'someValue'); params.set('param2', 'someValue'); return this.http.get(routes.Localization.Get, { search: params });