带括号的 axios paramsSerializer

axios paramsSerializer with brackets

我正在努力让我的 url

http://localhost:54700/api/patient/delete?id=5d43e980eb3b00005300493d

http://localhost:54700/api/patient/delete/5d43e980eb3b00005300493d

所以我尝试使用 paramsSerializer

  return await axios.get<any, T>(url, {
    params,
    paramsSerializer: params => {
      return queryString.stringify(params, {arrayFormat: 'brackets'});
    }
  });

和axios官方文档说的一样,但是

function stringify(obj?: ParsedUrlQueryInput, sep?: string, eq?: string, options?: StringifyOptions): string; 如您所见,第二个参数不是对象,而是字符串。

如何将我的 url 转移到 http://localhost:54700/api/patient/delete/5d43e980eb3b00005300493d

为什么要使用 paramsparamsSerializer 使事情过于复杂?我会写这样的东西:

const patientId = '5d43e980eb3b00005300493d';
const url = `http://localhost:54700/api/patient/delete/${encodeURIComponent(patientId)}`;
return axios.get(url);

就您最初的问题而言,我认为问题在于 Axios 示例使用的是 query-string NPM 包,而您正在尝试使用 querystring 核心节点库。这两个模块都提供 stringify() 函数,但接受不同的参数。