【问题标题】:Why is my nextjs router not return query params?为什么我的 nextjs 路由器不返回查询参数?
【发布时间】:2021-10-16 08:09:07
【问题描述】:

当我导航到我的 nextjs 应用程序的 http://localhost:3000/users/123?foo=bar 当我打印出router.query

时,我得到以下信息
{id: "123"}

没有将foo: 'bar' 添加到查询对象的原因可能是什么?

【问题讨论】:

  • 一般是你的路由路径定义的,能不能也放在这里?

标签: next.js nextjs-dynamic-routing


【解决方案1】:

您的file layout 应如下所示:

pages/
  users/
   [id].js

刚刚测试过了,返回的查询对象是{"foo":"bar","id":"123"}

【讨论】:

    【解决方案2】:

    我必须添加一个带有以下内容的 next.config.js 文件

    module.exports = {
      reactStrictMode: true,
      async rewrites() {
        return [
          {
            source: '/pages/:slug*',
            destination: '/:slug*'
          }
        ]
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-25
      • 2020-05-06
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 2018-09-18
      • 2019-05-21
      • 1970-01-01
      相关资源
      最近更新 更多