在 React Router 中获取当前路由的路径名 w/browserHistory

Obtaining pathname of current route in React Router w/browserHistory

对于使用 React Router 获取当前 url 路径名的最有效方法是什么(使用 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 也一样。

感谢任何建议,谢谢。

属性 this.props.location.pathname 将不会在 "various" 组件中可用,它只会在您的路由处理组件中可用。即 CustomerLookupAppCustomerLookupProfileWrapperCustomerLookupStoreProfileWrapperCustomerLookupStoreItemsWrapper

如果您需要路径名在树的更下方,那么您可以将其作为 props 传递下去。