【问题标题】:NextJS Dynamic routes parametersNextJS 动态路由参数
【发布时间】:2021-08-12 20:52:53
【问题描述】:

NextJS 是否有可能有一个动态路由匹配/country/country/city,但不匹配/country/city/whatever_content_here?我想为上述两条路线显示相同的页面,即使我只发送country 或同时发送countrycity

我知道您可以通过使用 [...country_city] 来做到这一点,但这也将匹配例如 /country/city/street,这不是所需的功能。

【问题讨论】:

    标签: javascript reactjs routes next.js


    【解决方案1】:

    是的,这是可能的。

    看这里的api参考:https://nextjs.org/docs/routing/dynamic-routes#catch-all-routes

    基于此,您可以编写逻辑来检查 country_city 数组并基于该显示内容

    【讨论】:

    • 没有解释我在问什么。 pages/post/[...slug].js matches /post/a, but also /post/a/b, /post/a/b/c and so on. 在我的示例中,我应该在 /post/apost/a/b 上匹配,但在 /post/a/b/c 上不匹配,它应该显示 Page Not Found
    • 这就是我的回应。您的查询将有一个像 json { "slug": ["a"] } json { "slug": ["a", "b"] } 这样的 json 对象。基于此,很明显,您只需评估列表的长度就可以使用简单的逻辑进行条件渲染?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 2022-06-12
    • 2020-10-16
    • 1970-01-01
    相关资源
    最近更新 更多