简体   繁体   English

NextJS 路由器使用嵌套页面未设置活动菜单

[英]NextJS router using nested pages not setting active menu

I am using NextJS and i am trying to set active menu items using the router.我正在使用 NextJS,我正在尝试使用路由器设置活动菜单项。 All 'top level' pages works fine, but if there is a page under the top level page, it does not set the active page.所有“顶级”页面都可以正常工作,但如果顶级页面下有页面,则不会设置活动页面。

router.pathname == "/profile" ? "active" : "not active"

The code above sets active for /profile but if for instance i have a page that is /profile/23543456 , then it sets to not active上面的代码为/profile设置了活动,但是如果我有一个页面是/profile/23543456 ,那么它设置为不活动

Is there a way to sort this out so that any sub pages of the profile also set active?有没有办法解决这个问题,以便配置文件的任何子页面也设置为活动状态?

Pathname field contains full route path.路径名字段包含完整的路由路径。 So in your case, router.pathname is /profile/23543456 .所以在你的情况下, router.pathname/profile/23543456 You can check if the pathname starts with /profile/ instead.您可以检查路径名是否以/profile/开头。

router.pathname.startsWith('/profile/') ? "active" : "not active"

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

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