加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

[react-router] hashHistory 和 browserHistory 的区别

发布时间:2020-12-15 20:17:41 所属栏目:百科 来源:网络整理
导读:react-router提供了三种方式来实现路由,并没有默认的路由,需要在声明路由的时候,显式指定所使用的路由。 //v1.xRouter///v2. 0.0 // hash history import { hashHistory } from ‘react-router‘ Router history={ hashHistory} / browserHistory hashHis

react-router提供了三种方式来实现路由,并没有默认的路由,需要在声明路由的时候,显式指定所使用的路由。

//v1.x
<Router/>
//v2.0.0 // hash history import { hashHistory } from ‘react-router‘ <Router history={hashHistory} />

  • browserHistory
  • hashHistory
  • createMemoryHistory

官方推荐使用browserHistory

使用hashHistory,浏览器的url是这样的:/#/user/liuna?_k=adseis

使用browserHistory,浏览器的url是这样的:/user/liuna

这样看起来当然是browerHistory更好一些,但是它需要server端支持。

使用hashHistory时,因为有 # 的存在,浏览器不会发送request,react-router 自己根据 url 去 render 相应的模块。

使用browserHistory时,从 / 到 /user/liuna,浏览器会向server发送request,所以server要做特殊请求,比如用的 express 的话,你需要 handle 所有的路由?app.get(‘*‘,(req,res) => { ... }),使用了 nginx 的话,nginx也要做相应的配置。让服务端都重定向到index.html页面

?

如果只是静态页面,就不需要用browserHistory,直接hashHistory就好了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读