2016-07-23 70 views
0

是否可以从外部组件获取当前路线?如何从反应路由器的外部获取当前路由?

基本上,我有一个只能显示当前页面标题的页面标题组件。似乎无法弄清楚如何从外部组件访问路由?

编辑: App.jsx

<div style={styles.container}> <Sidebar style={styles.sidebar} links={sidebarLinks}/> <div style={styles.wrapper}> <Header style={styles.header}/> <div style={styles.content}> <PageTitle style={styles.pageTitle} /> //Want current route here {this.props.children} </div> <Footer style={styles.footer}/> </div> </div> 路线

const routes = (
<Route path="/" component={App}> 
    <IndexRoute component={Dashboard}></IndexRoute> 
    <Route path="containers" component={Containers} /> 
    <Route path="discover" component={Discover} /> 
    <Route path="groups" component={Groups} /> 
    <Route path="profiles" component={Profiles} /> 
</Route> 
); 
+0

不知道你的意思究竟是什么,但看看这个可能? https://github.com/nfl/react-helmet – zackify

+0

@zackify请参阅编辑。基本上,我没有添加到页面标题组件的路由,因为它只是所有页面的页面标题。 –

+0

啊,我明白了。更好的方法是将“”组件放在特定的页面上。另一种方式,但不是那么好:React路由器会将路由对象注入到顶层组件,你可以像'routes [routes.length -1]这样做一些东西来获得我认为匹配的最后一条路径。 – zackify

回答

-1

不知道你是问。也许你可以使用window.location API:

window.location.href 

window.location.pathname