我是 Next.js 的新手,我很好奇如果我有这两条路线,其中一条是动态的,一条是原样的,并且两者的名称几乎相同:
/posts/[id]
/posts/latest
我在想他们会不会发生冲突?如果确实如此,那么 Next.JS 将如何区分 /latest
不是 /[id]
的参数?
最佳答案
Next.js 在内部处理它,使用预定义路由 /posts/latest
和动态路由 /posts/[id]
是完全可以的。在这种情况下,/posts/latest
将始终优先于动态路由。
来自dynamic routes documentation :
Predefined routes take precedence over dynamic routes, and dynamic routes over catch all routes. Take a look at the following examples:
pages/post/create.js
- Will match/post/create
pages/post/[pid].js
- Will match/post/1
,/post/abc
, etc. But not/post/create
pages/post/[...slug].js
- Will match/post/1/2
,/post/a/b/c
, etc. But not/post/create
,/post/abc
关于reactjs - Next.js 路线 : Dynamic vs exact,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69821141/