如何使用角度7发送带有带有逗号“,”的参数的URL

时间:2019-04-11 08:24:08

标签: angular api

我有一个值数组,我想发送一个get请求,该请求将把这些值作为参数,用逗号分隔,它应该看起来像这样

v1/public/users/en/search?users=user1,user2

component.ts

params = ['user1', 'user2'];

getUserProfile() {
  let httpHeaders = new HttpHeaders({ 'Content-Type': 'application/json' });
  const options = { headers: httpHeaders };
  return this.http.get('v1/public/users/en/' + this.queryUsers + this.params.join(","), options);
}

2 个答案:

答案 0 :(得分:0)

使用查询参数

getUserProfile() {
  let httpHeaders = new HttpHeaders({ 'Content-Type': 'application/json' });
  const query = {
      users=this.queryUsers.join(",")
  }
  const options = { headers: httpHeaders ,params:query};
  return this.http.get('http://95.177.160.97/v1/public/users/en/search', options)

答案 1 :(得分:0)

Angular有一个方便的东西叫HttpParams

params = ['user1', 'user2'];

getUserProfile() {
  let httpHeaders = new HttpHeaders({ 'Content-Type': 'application/json' });
  const httpParams = new HttpParams().set('users', params);
  const options = { headers: httpHeaders, params: httpParams };
  return this.http.get('v1/public/users/en/search', options);
}