2016-07-23 73 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