我想要检查路由是否存在于我的路由中,我如何使用matchPath,但它只匹配某个路径的模式,我想测试一个路由是否存在,有什么方法吗?
const handleNotifClick = (link, id) => {
setIsFetchingNotifs(true);
// check if "link"(pathname) exist in routes. what should i remplace "exist" with ??
if(exist) navigate(link);
else navigate('/access-denied');
if (acl?.notification?.put_notification_as_seen) axiosPut(`notification/mark-as-seen/${id}`);
};链接只是一个来自api的字符串,类似于'/products‘,我想检查该链接是否存在于我的路由中,这样我就可以导航到它,否则我想导航到拒绝访问。
exist只是一个占位符,它是我寻找的东西。
发布于 2022-11-14 04:04:04
我是用普通的js做的。
基本上,您可以使用window.location查找当前显示的路径:
const currentURL = window.location.href // returns the absolute URL of a page
const pathName = window.location.pathname //returns the current url minus the domain name然后,我这样做是为了不使用pathname显示基于路由的组件。
http://localhost:3000/create url:
. . .
{
pathName === "/create" ? (
""
) : (
<li className="nav-item">
<Link
className={`nav-link px-lg-3 py-3 py-lg-4 ${OtherClassName}`}
as={Link}
to="/create"
>
Create
</Link>
</li>
)}
...https://stackoverflow.com/questions/71323944
复制相似问题