Tenshi Asked:2020-06-20 19:21:55 +0000 UTC2020-06-20 19:21:55 +0000 UTC 2020-06-20 19:21:55 +0000 UTC 使用什么来获取当前 url 更好? 772 使用window.location.pathname或包装组件withRouter并从中获取路径会更好props吗?(用于不同的检查) reactjs 1 个回答 Voted Best Answer Denis Bubnov 2020-06-20T19:45:07Z2020-06-20T19:45:07Z 当然,我会推荐使用withRouter. 你正在使用React,所以使用它的辅助和方便的手段。我认为这里的选择很明确。 首先我们将它导入到我们的组件中: import { withRouter } from 'react-router-dom'; withRouter并在导出时包裹 export default withRouter(MyComponent) 然后我们在道具中拥有了我们需要的一切,比如this.props.history要么this.props.location要么this.props.match. 特别是因为您想将其用于不同的检查。在未来,它将非常有用。 文档链接: withRouter history location match
当然,我会推荐使用
withRouter. 你正在使用React,所以使用它的辅助和方便的手段。我认为这里的选择很明确。首先我们将它导入到我们的组件中:
withRouter并在导出时包裹然后我们在道具中拥有了我们需要的一切,比如
this.props.history要么this.props.location要么this.props.match. 特别是因为您想将其用于不同的检查。在未来,它将非常有用。文档链接:
withRouterhistorylocationmatch