首页 > 解决方案 > 如何在角度 7 中发送带有逗号“,”参数的 URL

问题描述

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

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

组件.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);
}

标签: angularapi

解决方案


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

推荐阅读