[react-router] hashHistory 和 browserHistory 的区别

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

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

  • browserHistory
  • hashHistory
  • createMemoryHistory

官方推荐使用browserHistorynginx

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

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

这样看起来固然是browerHistory更好一些,可是它须要server端支持。bash

使用hashHistory时,由于有 # 的存在,浏览器不会发送request,react-router 本身根据 url 去 render 相应的模块。react-router

使用browserHistory时,从 / 到 /user/liuna, 浏览器会向server发送request,因此server要作特殊请求,好比用的 express 的话,你须要 handle 全部的路由 app.get('*', (req, res) => { ... }),使用了 nginx 的话,nginx也要作相应的配置。app

 

若是只是静态页面,就不须要用browserHistory,直接hashHistory就行了。ui

相关文章
相关标签/搜索