0
在我有<Route>
代碼的SideMenuNavigation組件中,我需要訪問this.props.location.pathname
的值。不幸的是,我得到undefined
。在路由組件中訪問this.props.location.pathname
class SideNavigation extends React.Component {
componentDidMount() {
console.log(this.props.location.pathname); // undefined
}
render() {
return(
<Router>
<Route exact path="/" component={Dashboard}/>
</Router>
)
}
}
/* */
在我的儀表板組件中,我可以成功訪問this.props.location.pathname
。不幸的是,我不需要這個組件。我需要SideMenuNavigation
組件中的值。
我得到它與使用withRouter和無路徑所以'出口默認與路由器(SideNavigation)'和<路由組件= {SideNavigation} />它已修復! :) –
devwannabe