如何从React-Router中的URL中删除哈希

丹尼斯·内鲁什(Dennis Nerush):

我正在使用react-router进行路由,并且使用hashHistory选项,以便可以从浏览器刷新页面或指定现有路由之一的url并到达正确的页面。它工作正常,但我在URL中看到的哈希是这样的:http:// localhost /#/ login?_k = ya6z6i

这是我的路由配置:

ReactDOM.render((
 <Router history={hashHistory}>
    <Route path='/' component={MasterPage}>
      <IndexRoute component={LoginPage} />
      <Route path='/search' component={SearchPage} />
      <Route path='/login' component={LoginPage} />
      <Route path='/payment' component={PaymentPage} />
    </Route>
  </Router>),
    document.getElementById('app-container'));
Aaleks:

您是否尝试过browserHistory选项?您还可以从浏览器刷新页面,或指定现有路线之一的网址并着陆在正确的页面上。

import { Router, Route, browserHistory } from 'react-router';

ReactDOM.render((
 <Router history={browserHistory}>
    <Route path='/' component={MasterPage}>
      <IndexRoute component={LoginPage} />
      <Route path='/search' component={SearchPage} />
      <Route path='/login' component={LoginPage} />
      <Route path='/payment' component={PaymentPage} />
    </Route>
  </Router>),
    document.getElementById('app-container'));

此外,考虑到react-router github文档,hashHistory不适用于生产。

https://github.com/ReactTraining/react-router/blob/master/docs/guides/Histories.md#browserhistory

我应该使用hashHistory吗?

哈希历史记录无需配置服务器即可工作,因此,如果您刚刚入门,请继续使用它。但是,我们不建议在生产环境中使用它,每个Web应用程序都应渴望使用浏览器。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章