我有一个BrowserRouter
来自“react-router-dom”v4的简单应用程序。我试图location.pathname
从<BrowserRouter/>
组件内部访问该属性,但无济于事:
class App extends Component{
render(){
return (
<BrowserRouter>
// How do I access this.props.location?
<div className={(this.props.location.pathnme === "/account") ? "bgnd-black" : "bgnd-white"} >
<Switch>
<Route path="/login" component={LoginPage}/>
<Route path="/success" component={LoginSuccess}/>
<Route path="/account" component={MyAccount}/>
...
<Route component={Error404}/>
</Switch>
</div>
</BrowserRouter>
);
}
}
我知道我可以通过子组件访问应用程序的当前路径位置this.props.location.pathname
,但我需要从父组件访问它,就在下面,<BrowserRouter/>
以运行与子组件无关的附加逻辑。我怎样才能得到这个位置?