简体   繁体   English

Next Js中的动态路由返回404然后重新加载

[英]Dynamic routing in Next Js return 404 and then reloads

Whenever the user clicks on the button I need to redirect to /solutions/[id] where id is dynamic.每当用户单击按钮时,我都需要重定向到 /solutions/[id] ,其中 id 是动态的。 Tried using Link and next router.尝试使用链接和下一个路由器。 If I click that button its failing for the first time(returns 404) and then automatically page reloads.如果我第一次单击该按钮失败(返回 404),然后自动重新加载页面。 Is this expected behaviour?这是预期的行为吗? How do i avoid this?我如何避免这种情况?

<Link href="/quiz/solutions/a">
     <a>First comment</a>
</Link>  

My folder structure我的文件夹结构

pages
    -solutions
          -[index].js

On Next versions prior to 10, you have to use the href and as props.在 10 之前的 Next 版本中,您必须使用hrefas道具。 Check out this issue for more information.查看此问题以获取更多信息。 Here is a link to the old documentation as well, the relevant part of the docs state: 这里也是旧文档的链接,文档的相关部分说明:

href is a file system path used by the page and it shouldn't change at runtime. href是页面使用的文件系统路径,它不应在运行时更改。 as on the other hand, will be dynamic most of the time according to your needs. as在另一方面,将根据您的需求是动态的大部分时间。

<Link href="/quiz/solutions/[id]" as="/quiz/solutions/a">
  <a>First comment</a>
</Link> 

This is assuming that the page is [id].js inside the solutions folder.这是假设页面是解决方案文件夹中的[id].js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM