2017-02-27 29 views
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組件中的值。

回答

0

location是由路由器組件注入的道具之一。在你的情況下,SideNavigation不是路由器組件的子節點(實際上它是父節點)。你可能也對這個問題感興趣:How does react-router pass params to other components via props?

+0

我得到它與使用withRouter和無路徑所以'出口默認與路由器(SideNavigation)'和<路由組件= {SideNavigation} />它已修復! :) – devwannabe