2

单击 next.js 中的按钮后,我想在 url 参数中添加数据

  function send() {
    router.push(
      { pathname: "/", query: { n: "firstparam,secondparam" } },
      undefined,
      {
        shallow: true
      }
    );
  }

点击后,我进入网址:

site/?n=firstparam%2Csecondparam

因此, nextjs 而不是,, 添加%2C. 如何避免所有这些迹象并在不使用replace()?类似的情况下获得有效的网址:site/?n=firstparam,secondparam
演示:https ://codesandbox.io/s/vibrant-sinoussi-d9z77?file=/pages/index.js

4

1 回答 1

3

%2C是 的 url 编码,,Next.js 所做的是正确的做法,因为,它不是有效字符。

console.log(encodeURIComponent(','))

您可以尝试将 url 作为字符串传递。

function send() {
  router.push('/?n=firstparam,secondparam', undefined, {
    shallow: true,
  });
}
于 2020-10-27T13:30:35.053 回答