Angular 8为特殊字符获取用%编码的URL参数(HttpParams)



我正在搜索取消angular的HttpParams的默认解码:

在URL中:?类别=第一个%2秒

然后当我得到HttpParams并调用具有类别的API时:?类别=第一个+第二个

但我不希望%2B被这样编码,我需要用%2B 调用API

关于这个问题有很多主题,但都是手动设置参数,而不是我,我得到参数并在后调用API

我找到了encodeURIComponent,但我真的不知道如何获得params并重新编码它们

我试试这个:

if (params) {
params.keys().forEach((key: string) => {
if(key == 'categories'){
console.log("i'm here");

}
request[key] = params.get(key);
});
}

但在这种情况下,我不知道如何重新编码参数

Ty:(

这实际上是有效的:

console.log(params);
const categories = [];
let newParams = new HttpParams();
let test = new HttpParams();
if (params) {
params.keys().forEach((key: string) => {
if(key == "categories") {
for (let index = 0; index < params.get(key).length; index++) {
categories.push(encodeURIComponent(params.get(key)[index]));
}
newParams = params.delete(key);
categories.forEach(categorie => {
newParams = newParams.append(key, categorie);
});
console.log(newParams);
}
});
}

但当我使用新的类似的参数时:

const url = `${BASE_URL}/${`api/${id}/random`}`;
return this.http.get<Page<Element>>(url, {params});

在api调用中变为%252之前已在%2B中正确编码的"+">

最新更新