angular - 如何在角度 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);
}
解决方案
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);
}
推荐阅读
- android - ASyncTask ConnectException:连接被拒绝
- html - 调整页面大小仅在某些时候有效
- react-native - 如何获取文本或按钮以在世博会地图上注册点击事件?
- javascript - 如何从字符串数组创建 Chrome 扩展上下文子菜单项
- python - 每行具有不同标准偏差的 Numpy 数组
- reactjs - 通过 ESLint 版本在 vscode 上启动 React 应用程序失败
- javascript - 新项目进入时将状态更改为true并在5秒后更改为false
- python - TensorFlow WALS 矩阵分解估计器的 export_savedmodel() 如何正确?
- python - 我在扫描字符串文字时遇到语法错误,说 EOL 为什么?
- python - 将 while 循环值插入到 tkinter 魔术窗口