2017-02-27 170 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