简体   繁体   English

反应路由器哈希历史记录

[英]React router hash history

I was study about hashHistory vs browserHistory and stuck on the point 'Hash history not required any server side configuration'. 我研究了hashHistory与browserHistory并停留在“哈希历史不需要任何服务器端配置”这一点上。 I didn't understand about this point, Please let me know. 我不明白这一点,请告诉我。

When you setup react-router to use 'hash history'... 当您将react-router设置为使用``哈希历史记录''时...

 <Router history={hashHistory}>

... it adds these strange looking # strings (hash strings) at the end of your URLs. ...它会在网址末尾添加这些看起来很奇怪的#字符串(哈希字符串)。 The router uses the information contained in the string to render the correct components for the particular page that is requested. 路由器使用字符串中包含的信息来为请求的特定页面呈现正确的组件。

If you don't want the hash strings appearing at the end of your URL, you can setup react-router to create real URLs that look like example.com/some/path . 如果您不希望哈希字符串出现在URL的末尾,则可以设置react-router来创建类似于example.com/some/path真实URL。 Internally react-router will make sure the correct components for that route are rendered. 内部react-router将确保呈现该路由的正确组件。

There is only one problem. 只有一个问题。 Now if someone accesses example.com/some/path your server will try to find that page in your website directory instead of handing the request over to your react router. 现在,如果有人访问example.com/some/path您的服务器将尝试在您的网站目录中找到该页面,而不是将请求移交给您的React Router。 Your server doesn't know how to handle the URL. 您的服务器不知道如何处理URL。

You will have to configure the server to always deliver your app no matter what URL comes in. Then your app, in the browser, will manipulate the URL to return the correct page. 无论输入什么URL,都必须将服务器配置为始终交付您的应用程序。然后,您的应用程序在浏览器中将操纵URL返回正确的页面。 That's why you will also need some configuration on the server, when you want to use 因此,当您要使用服务器时,还需要在服务器上进行一些配置

 <Router history={browserHistory}>

for better looking urls 获得更好的网址

Here is some info on the difference between browser history (nice looking url's) and hash history (# sign in the url) 以下是有关浏览器历史记录(网址看起来不错)和哈希历史记录(网址中的#号)之间的区别的一些信息

You don't write what server you are using. 您无需编写正在使用的服务器。 There are some server configuration examples (nginx, apache) in the documentation. 文档中有一些服务器配置示例(nginx,apache)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM