【问题标题】:Next.js does not pass path paramNext.js 不传递路径参数
【发布时间】:2020-10-20 19:24:07
【问题描述】:

我在 Next.js 版本 9.4.4 中开发了一个应用程序。我的文件夹结构如下图:

pages
[id]
  index.js
index.js

当我向localhost:3000/something 发出请求时,它会转到索引页面,当我在根索引页面中记录此信息时,我无法获取/这里是根索引文件的代码

Index.getInitialProps = (ctx) => {
  console.log(ctx.headers, ctx.pathname, ctx.url);

  return {};
};

输出是

undefined / undefined

【问题讨论】:

  • console.log(ctx) 显示什么?
  • @SterlinV 这是 ctx 输出的链接justpaste.it/2lr8s
  • @RasoulSalehi 你在当前页面添加了 getInitialProps 吗?
  • @visizky 我不知道您所说的当前页面是什么意思,如果您的意思是我添加了,则在 id 页面中并且没有记录任何内容,因为它转到根索引页面,这是我添加 getIntialProps 的地方是该页面上的代码。并且都有 getInitialProps
  • 我不知道为什么 /something 没有在 id 页面或索引页面的任何地方传递

标签: next.js


【解决方案1】:

您正在使用动态路由。因此,您的“路径”将在 ctx.query 中(在您的情况下:ctx.query.id)

【讨论】:

猜你喜欢
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-12
  • 2021-12-02
  • 2018-12-26
  • 2020-10-23
相关资源
最近更新 更多