如何使用Next.js中的动态路线路由到页面
我试图将这些URL路由到[id] .js file。想。是否可以使用相同的[ID] .JS文件?对于URL 1。
url 1 -> /settings
url 2 -> /settings/notifications
url 3 -> /settings/security
我的文件结构
settings > [id].js
获取ID
const router = useRouter()
const { id } = router.query;
I am trying to route these urls to [id].js file in next.js, So everything is working for url 2 and 3 below, but its not working for url 1 so I have to create another index.js file which I dont want. Is is possible to use the same [id].js file? for url 1 also.
url 1 -> /settings
url 2 -> /settings/notifications
url 3 -> /settings/security
my file structure
settings > [id].js
getting the id
const router = useRouter()
const { id } = router.query;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面的文件结构是一种方法:
但是,如果您不想添加索引文件,则可以使用可选的 catch-all路线功能。请注意,它附带了您可能不想要的其他功能,这也可能会更改您的代码。
这是您必须遵循的结构:
A file structure like the below is one way to do it:
But if you do not want to add an index file you can make use of optional catch-all routes feature. Please note that it comes with other features you might not want and this might change your code a little too.
This is the structure you will have to follow: