Next js中具有多个参数的动态路由 [英] Dynamic routing with multiple parameters in Next js

查看:35
本文介绍了Next js中具有多个参数的动态路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm using Next js. I want to use dynamic routing based on this documentation. How can I set two parameters like href="/page/[p1]/[p2]" to the Link component? What should be the structure of my page file and how use the router.query?

解决方案

You can do this with the help of dynamic routing in nextjs 9. For example, pages/post/[postId]/[commentId].js would match /post/p1/c1. Its query object would be: { postId: 'p1', commentId: 'c1' }. your Link component should be like this:

<Link
   href="/post/[postId]/[commentId]"
   as={`/post/${postId}/${commentId}`}>
      <a>link to comment</a>
</Link>

这篇关于Next js中具有多个参数的动态路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆