【问题标题】:How to remove the hash from the url in react-router如何从 react-router 中的 url 中删除哈希
【发布时间】:2016-05-12 22:35:52
【问题描述】:

我正在使用 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'));

【问题讨论】:

    标签: reactjs react-router


    【解决方案1】:

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

    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 doc,hashHistory 不适用于生产用途。

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

    我应该使用 hashHistory 吗?

    哈希历史不需要配置你的服务器,所以如果你只是 开始,继续使用它。但是,我们不建议使用它 在生产环境中,每个网络应用都应该渴望使用 browserHistory

    【讨论】:

    • 我试过了,哈希消失了。但是浏览器刷新后我无法登陆同一页面
    • 您是否使用服务器 (node.js) 来提供您的文件?
    • 如果您第一次向服务器发出请求,假设您在 mysite.com 上单击调用路由器 mysite.com/page1 的按钮时提供 index.html 文件,它应该可以工作它将在客户端使用反应路由器显示页面。但是,当您刷新它时,它会询问服务器 URL mysite.com/page1 但是如果您为每个请求提供 index.html 文件,并且在每个请求上使用 * 之类的模式,我都会提供 index.html 您应该看到正确的内容,因为 mysite. com/page1 -> 提供 index.html 但反应路由器看到 /page1 并像您在 page1 按钮上再次单击自己一样完成工作。
    • browserHistory 需要服务器端渲染。所以我在生产中使用hashHistory,因为我的项目中不需要服务器端渲染。
    • 如果您想将react-router-domhistory 一起使用,请使用:import createHistory from 'history/createBrowserHistory';
    【解决方案2】:

    我是新手,但我使用过 BrowserRouter,它工作正常:-

        import React from "react";
        import ReactDOM from "react-dom";
        import { BrowserRouter, Route, Switch } from "react-router-dom";
    
    ReactDOM.render(
      <BrowserRouter >
        <Switch>
          {indexRoutes.map((prop, key) => {
            return <Route to={prop.path} component={prop.component} key={key} />;
          })}
        </Switch>
      </BrowserRouter>,
      document.getElementById("root")
    );
    

    【讨论】:

      【解决方案3】:

      我相信 Dennis Nerush 在上面提到过,您需要使用 {browserHistory} 而不是 {hashHistory},但是要使相同的页面呈现工作,您需要稍微配置您的服务器以允许这样做。

      根据您的托管位置或您使用的服务器,有几种方法可以做到这一点

      对于 Apache,您必须将以下内容添加到 .htaccess(或创建 .htaccess 并将其放在您网站的根文件夹中):

      Options -MultiViews
      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^ index.html [QSA,L]
      

      对于 Node.js/ Express 需要添加以下代码:

      app.get('/*', function(req, res) {
        res.sendFile(path.join(__dirname, 'path/to/your/index.html'), function(err) {
          if (err) {
            res.status(500).send(err)
          }
        })
      })
      

      对于 Nginx 服务器,您需要将以下内容添加到 Nginx.conf 文件中

      location / {
        if (!-e $request_filename){
          rewrite ^(.*)$ /index.html break;
        }
      }
      

      对于 Amplify,您需要转到 Rewrites & Redirects 并添加一个包含以下信息的新规则(注意我只在 AWS 上使用过):

      Source address: </^[^.]+$|\.(?!(css|gif|ico|jpg|js|png|txt|svg|woff|ttf)$)([^.]+$)/>
      Target address: /index.html
      Type: 200
      

      如果您想对该主题进行更多研究,请点击以下链接。

      https://www.andreasreiterer.at/fix-browserrouter-on-apache/#comment-186(专门用于 Apache)

      https://ui.dev/react-router-cannot-get-url-refresh/(不同服务器的多种方法或没有我上面没有添加的服务器)

      React Router DOM not working correctly on Amplify Console AWS(将这个用于 AWS 和 Amplify)

      【讨论】:

        【解决方案4】:

        您需要从react-router 导入browserHistory
        并将其传递给&lt;Router /&gt;,以便从 URL 中删除哈希

        例如:

        import { browserHistory } from 'react-router';
        
        <Router history={browserHistory}>
         //Place your route here
        </Router>
        

        【讨论】:

        • 模块 '"react-router"' 没有导出成员 'browserHistory'.ts(2305)
        【解决方案5】:

        试试这个:

        // v1.x
        import createBrowserHistory from 'history/lib/createBrowserHistory'
        <Router history={createBrowserHistory()} />
        
        // v2.0.0
        import { browserHistory } from 'react-router'
        <Router history={browserHistory} />
        
        
        const history = createHashHistory({ queryKey: false });
        <Router history={history}>
        </Router>
        

        https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#using-custom-histories

        https://github.com/reactjs/react-router/blob/8ef625e8fa7c41dda0f3d1d68a10b74bd93a4101/docs/guides/ba...

        【讨论】:

        • 猜猜为什么我在没有使用 react-router 的情况下会出现这个错误?只是反应和反应-dom
        • 3.x 怎么样? You have provided a history object created with history v2.x or earlier. This version of React Router is only compatible with v3 history objects. Please upgrade to history v3.x.
        • ok 修复了这个import { createHistory } from 'history'; // you need to install this package let history = createHistory();
        猜你喜欢
        • 1970-01-01
        • 2016-06-13
        • 2011-05-29
        • 2012-04-19
        • 2013-05-15
        • 2013-03-02
        • 1970-01-01
        • 2021-02-24
        相关资源
        最近更新 更多