使用React路由器dom获取完整url



我需要在<MetaTags />React组件中返回完整的url。

因此,我能够将正确的完整url<link rel="canonical" href={-> correct url here} />传递给这个元标记。

react-router-dom我得到了相对路径:

const location = useLocation();
console.log(location.pathname) // /my-path 

唯一的方法是window.location.href,还是使用React路由器也可以获得绝对url?

您真的需要完整的url吗?您可以简单地使用react路由器dom提供的<Link />

在我看来,为了在应用程序内部重定向,你应该使用<Link to="">,当它在应用程序外部时使用href

最新更新