简体   繁体   English

如何让 NextJS 拾取多个动态路由?

[英]How to get NextJS to pickup multiple dynamic routes?

I'm using a dynamic page creation through NextJS called我正在使用名为 NextJS 的动态页面创建

[video].tsx

The dynamic pages are created as:动态页面创建为:

 const Video = (props) => {
 const router = useRouter()
 const { video } = router.query
 const videoData = GeneralVideo.find((d) => d.link === video)

The linking is setup as:链接设置为:

<Link  href={'/videos/' + item.link} passHref={true}>

Dynamic pages are then successfully created with URLs such as:然后使用以下 URL 成功创建动态页面:

http://localhost:3000/videos/100004_SF

and the [video].tsx page is correctly picked up.并且 [video].tsx 页面被正确拾取。

Now , I want to set up a second set of dynamic pages as现在,我想将第二组动态页面设置为

[early].tsx

Dynamic pages are then created as:然后将动态页面创建为:

 const Early = (props) => {
 const router = useRouter()
 const { early } = router.query
 const videoData = GeneralVideo.find((d) => d.linkEarly === early)

The linking is setup as:链接设置为:

 <Link href={'/videos/' + item.linkEarly} passHref={true}>

Now, dynamic links are successfully generated as现在,动态链接成功生成为

http://localhost:3000/videos/100159_SF_early

But the [early].tsx component is not picked up correctly delivering an empty page.但是 [early].tsx 组件没有被正确拾取并提供一个空页面。

Where is my mistake in the setup?我的设置错误在哪里?

Lets say you create two dynamic routes, /videos/[video] and /video/[early], you may think that these two represent different routes because the dynamic parameter being used has different names, but for NextJS, its the same route.假设您创建了两个动态路由,/videos/[video] 和 /video/[early],您可能会认为这两个表示不同的路由,因为正在使用的动态参数具有不同的名称,但对于 NextJS,它是相同的路由。 So there are two ways of going about it:所以有两种方法可以解决它:


Method 1方法一

Change your dynamic routes to /videos/normal/[link] and /videos/early/[link]将动态路由更改为 /videos/normal/[link] 和 /videos/early/[link]


Method 2方法二

In your original route ie /videos/[link], pass different url parameters, those that come after a question mark , for the two categories of videos and then use it inside your router query to render components accordingly.在您的原始路由(即 /videos/[link])中,为两类视频传递不同的 url 参数,即问号之后的参数,然后在路由器查询中使用它来相应地渲染组件。


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

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