2016-11-03 138 views
1

在使用React Router獲取當前URL的路徑名的最有效方式是(使用browserHistory)的各種源之間似乎存在一些不一致。下面是我的供應商和路線的一個片段:在React Router/browserHistory中獲取當前路由的路徑名

ReactDOM.render(
     <Provider store={Store}> 
      <Router history={browserHistory}> 
       <Route path="/tools/customer-lookup" component={CustomerLookupApp} /> 
       <Route path="/tools/customer-lookup/profile/:id" component={CustomerLookupProfileWrapper} /> 
       <Route path="/tools/customer-lookup/profile/:id/store/:storeid" component={CustomerLookupStoreProfileWrapper} /> 
       <Route path="/tools/customer-lookup/profile/:id/store/:storeid/transaction/:transid" component={CustomerLookupStoreItemsWrapper} /> 
      </Router> 
     </Provider>, 
     document.getElementById('customer-lookup-main')); 
}); 

登錄嘗試各種成分this.props.location.pathname但總是出現不確定的。與this.context一樣的東西。

任何意見表示讚賞,謝謝。

+0

你的代碼似乎是正確的,除了你的路線路線有點多餘。你可以使用子路徑來減少'path'的長度。您可以發佈完整的代碼,以便調試.red –

+0

@JasonXu除了我試圖**避免**在旅行到兒童路線時將一個組件嵌套在另一個組件中。相反,路線(雖然我同意他們看起來非常冗餘)鏈接到一個完全不同的單頁面應用程序的視圖,並不一定重用任何父組件。如果有更好的方法來完成**並且仍然嵌套路線,我很有興趣聽到它。 – Jose

回答

0

該屬性this.props.location.pathname將不會在「各種」組件中可用,它只會在您的路由處理組件中可用。即CustomerLookupApp,CustomerLookupProfileWrapper,CustomerLookupStoreProfileWrapper,CustomerLookupStoreItemsWrapper

如果你需要進一步在樹下的路徑名,那麼你可以把它作爲道具傳遞下去。

+0

我仍然可以通過它嗎? ''我在'this'上得到了一個undefined。 – Jose