在我的组件之外,我需要查询当前事件的 URL。我将基于此在主体(在我的 react 根之外)上设置一些类。
第一次尝试是使用
//Gets an array of class names that I can add to my body tag
getClassNames(window.location.pathname);
但似乎 window.location.path 在 React Router 导航时没有更新。令人惊讶的是。
所以我想,好吧,也许我可以从 browserHistory 中获取它
import { browserHistory } from 'react-router'
但是,唉,我也看不到从这里读取当前页面路径的方法(这个对象似乎不存在合适的 API 文档)
有什么建议吗?这似乎是一个简单的问题,如果 window.location.pathname 与历史对象保持同步。
最佳答案
至少从 2017 年开始,可以通过
获得位置browserHistory.getCurrentLocation();
const location = browserHistory.getCurrentLocation();
console.log(location);
关于javascript - 从 react-router 浏览器 History 对象获取当前页面路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36192003/