您不能对同一动态路径 Nextjs 使用不同的 slug 名称 [英] You cannot use different slug names for the same dynamic path Nextjs

查看:15
本文介绍了您不能对同一动态路径 Nextjs 使用不同的 slug 名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用了基于文件夹结构的默认动态路由技术 nextjs.我有一条路线:

I'm using the default dynamic routing technique of nextjs in my project which is based on the folder structure. I have a route which is:

pages/[language]/location/[location_id]

现在我遇到了一个用例,除了 [location_id] 的最后一个参数之外,我需要上述路由完全相同,我需要 [route_id] 这里.

Now I'm coming across a use case where I need the above route exactly the same except the last parameter of [location_id], I need [route_id] here.

当我为这条新路线创建文件 [route_id].js 时,我遇到了这个错误:

When I created a file [route_id].js for this new route, I'm facing this error:

 throw new Error(`You cannot use different slug names for the same dynamic path ('${previousSlug}' !== '${nextSlug}').`);

在进行一些研究后,我知道错误以及为什么它向我显示此问题的问题,但我无法理解如何解决此问题.我只想要一个解决方案,如何在我的应用中实现这两条路线:

I'm aware of the error and the problem about why it's showing me this after doing some research but I'm unable to understand how I can solve this problem. I just want a solution how I can implement these two routes in my app:

Route 1: pages/[language]/location/[location_id]

Route 2: pages/[language]/location/[route_id]

PS:我已经研究过这个:https://github.com/zeit/next.js/issues/9081

PS: I've already studied this: https://github.com/zeit/next.js/issues/9081

推荐答案

你提供的两条路线是完全等价的,完全没有区别.因此,它们将由单个页面处理.对于上述两个路由,没有两个 url 将路由到不同的 nextjs 页面的示例.

The two routes you have provided are exactly equivalent and are not at all different. Therefore, they will be handled by a single page. There is no example of two urls that will route to different nextjs pages for the above two routes.

[] 编写的任何内容都可以处理任何类型的值,例如 1 或 2 或任何其他值.

Anything written in [] will handle any type of value e.g 1 or 2 or any other value for that matter.

您不能创建两个页面来处理相同的请求,因为 next 或其他任何人都无法知道您使用的是 route_id 还是 location_id,因为它们都是代表任何值的变量.

You can not make two pages to handle same request because there is no way for next or anyone else for that matter to know whether you are using route_id or location_id because these are both variables that are representing any value.

如果您想区分它们,请创建一条新路线

If you want to differentitate between them, either create a new route with

/route/[route_id]而不是

/location/[location_id],或使用 queryparams 例如

pages/[language]/location?locationid=[location_id]

pages/[language]/location?routeid=[route_id]

这篇关于您不能对同一动态路径 Nextjs 使用不同的 slug 名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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