Tho*_*len 1 javascript directory next.js
我一直在阅读 Next.js 文档,并认为我了解 [slug].js 动态路由过程,但我很难理解文件夹结构方面的嵌套动态路由。
如果我想制作一个基于用户的应用程序,我将如何实现/user/[userid]/post/[postId]?
并且想要这样的东西:
user
- [id].js // e.g. user/1
- [userId]
- - post
- - - [postId].js // e.g. user/[userId]/post/[postId]
Run Code Online (Sandbox Code Playgroud)
但这会引发关于 [slugs] 的错误,因为我认为在同一个文件夹中不能有两个 slug。
任何人都可以解释正确的文件夹结构来实现这一目标吗?非常感谢任何帮助。
而不是[id].js创建一个index.js在[userId]文件夹中命名的文件,该文件将用于呈现路由路径的页面/user/[userId]/。
pages/
user/
[userId]/
- index.js // will match for /user/1234
post/
- index.js // will match for /user/1234/post
- [postId].js // will match for /user/1234/post/some-post
Run Code Online (Sandbox Code Playgroud)
同样,创建一个index.jsunder 文件夹post将有助于匹配路由路径/user/[userId]/post/,该路径可用于显示该用户的帖子列表。
一个例子与用于类似用途的情况下的文件夹结构。