带括号的 axios paramsSerializer [英] axios paramsSerializer with brackets
问题描述
我正在尝试制作我的网址
I've trying to make my url
http://localhost:54700/api/patient/delete?id=5d43e980eb3b00005300493d
到
http://localhost:54700/api/patient/delete/5d43e980eb3b00005300493d
所以我尝试使用 paramsSerializer
So I tried using paramsSerializer
return await axios.get<any, T>(url, {
params,
paramsSerializer: params => {
return queryString.stringify(params, {arrayFormat: 'brackets'});
}
});
和axios官方文档说的一样,但是
Same as the axios official document says, but
function stringify(obj?: ParsedUrlQueryInput, sep?: string, eq?: string, options?: StringifyOptions): string;
如您所见,第二个参数不是对象,而是字符串.
function stringify(obj?: ParsedUrlQueryInput, sep?: string, eq?: string, options?: StringifyOptions): string;
as you can see second parameter is not an object, but string.
如何将我的网址转移到http://localhost:54700/api/patient/delete/5d43e980eb3b00005300493d
?
How to transfer my url to http://localhost:54700/api/patient/delete/5d43e980eb3b00005300493d
?
推荐答案
为什么你需要用 params
和 paramsSerializer
使事情变得过于复杂?我只想写这样的东西:
Why do you need to overcomplicate things with params
and paramsSerializer
? I would just write something like this:
const patientId = '5d43e980eb3b00005300493d';
const url = `http://localhost:54700/api/patient/delete/${encodeURIComponent(patientId)}`;
return axios.get(url);
就您最初的问题而言,我认为问题在于 Axios 示例使用的是 query-string
NPM 包,而您正在尝试使用 querystring
核心 Node 库.这两个模块都提供了一个 stringify()
函数,但接受不同的参数.
As far as your original problem, I think the issue is that the Axios example is using the query-string
NPM package, while you're using attempting to use the querystring
core Node library. Both of those modules provide a stringify()
function but accept different parameters.
这篇关于带括号的 axios paramsSerializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!