gpt4 book ai didi

reactjs - 检查 React 中嵌套路由的 router.isActive

转载 作者:行者123 更新时间:2023-12-03 13:31:18 26 4
gpt4 key购买 nike

我正在尝试使用 Router.isActive 检查路由

如果当前路径是/animals/edit

(router.isActive('/animals/edit')

是真的。

但是如果当前路线是/animals/edit/23

(router.isActive('/animals/edit/') 

是假的。

如何创建包含尾随参数的路由?

最佳答案

我也遇到了同样的问题。通过使用router.getCurrentLocation().pathname匹配路由来解决这个问题。

在您的示例中,应执行以下操作:

const parentPathname = '/animals/edit';
const currentPathname = router.getCurrentLocation().pathname;
const isActive = currentPathname.indexOf(parentPathname) !== -1;

关于reactjs - 检查 React 中嵌套路由的 router.isActive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43750056/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com