Javascript react router中的hashHistory和browserHistory有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36289683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:52:48  来源:igfitidea点击:

What is the difference between hashHistory and browserHistory in react router?

javascriptreactjsreact-router

提问by Ben Bieler

I have googled quite some bit, but I didn't find a clear answer to the following question: What is the difference between hashHistory and browserHistory in react-router?

我在谷歌上搜索了很多,但我没有找到以下问题的明确答案:react-router 中的 hashHistory 和 browserHistory 之间有什么区别?

采纳答案by smcdrc

The basic difference is that the hashHistory uses URLs like: http://myurl.com/#page/another_page/another_page

基本区别在于 hashHistory 使用的 URL 如下:http://myurl.com/#page/another_page/another_page

With BrowserHistory you get normal urls (no hash): http://myurl.com/page/another_page/another_page

使用 BrowserHistory,您可以获得正常的网址(无哈希):http: //myurl.com/page/another_page/another_page

回答by luanped

I don't think the question was asking for differences in the format, but rather technical. Hence sharing this answer here with a technical difference: https://stackoverflow.com/a/42157741/2445694

我认为问题不是要求格式上的差异,而是技术上的差异。因此,在这里分享这个具有技术差异的答案:https: //stackoverflow.com/a/42157741/2445694

Basically the browser don't send the url after the #

基本上浏览器不会在 # 之后发送 url

So suppose that a website restricted areas for members and admins. A user navigates to /member, and is prompted for logging in. However the server won't know if the user was trying to access /admin or /member before getting on the log in page, so after logging in the server don't know where to redirect.

所以假设一个网站限制了成员和管理员的区域。用户导航到 /member,并提示登录。但是,在登录页面之前,服务器不会知道用户是尝试访问 /admin 还是 /member,因此在登录服务器后不要知道重定向到哪里。

回答by Bojan Golubovic

First difference:

第一个区别:

They are using different WEB APIs. <HashRouter>uses and reads the hash from URL, <BrowserRouter>uses window.historyWEB API.

他们使用不同的 WEB API。 <HashRouter>使用并从 URL 读取哈希, <BrowserRouter>使用window.historyWEB API。

Second difference:

第二个区别:

<HashRouter>is used for static one-page website. Ideal for browser based projects. <BrowserRouter>is used for dynamic website. Should be used when you have a server that will handle dynamic requests (knows how to respond to any possible URL).

<HashRouter>用于静态单页网站。非常适合基于浏览器的项目。 <BrowserRouter>用于动态网站。当你有一个处理动态请求的服务器(知道如何响应任何可能的 URL)时应该使用。

回答by MERLIN THOMAS

1) Browser's history's location array contains more than just the locations that have been visited within our application. Allowing access to this list would leak information about a user's browsing history that websites should not be allowed access to.

1) 浏览器的历史位置数组包含的不仅仅是在我们的应用程序中访问过的位置。允许访问此列表会泄露有关不应允许网站访问的用户浏览历史记录的信息。

2) Browser history creates location objects whose pathname is the URL's full pathname. However, you can specify a basename for a history, in which case a portion of the full pathname will be effectively ignored.

2) 浏览器历史创建位置对象,其路径名是 URL 的完整路径名。但是,您可以为历史指定一个基本名称,在这种情况下,完整路径名的一部分将被有效地忽略。

3) Browser History in static file server will have one real location on our server to fetch our HTML from while Hash history uses the hash section of the URL to set and read locations.

3) 静态文件服务器中的浏览器历史将在我们的服务器上有一个真实的位置来获取我们的 HTML,而哈希历史使用 URL 的哈希部分来设置和读取位置。

4) Hash History is reliant as it store all of the path information in the hash of a URL.

4) 哈希历史是依赖的,因为它将所有路径信息存储在 URL 的哈希中。