[react-router] hashHistory 和 browserHistory 的區別

react-router提供了三種方式來實現路由,並無默認的路由,須要在聲明路由的時候,顯式指定所使用的路由。html

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

  • browserHistory
  • hashHistory
  • createMemoryHistory

官方推薦使用browserHistoryreact

使用hashHistory,瀏覽器的url是這樣的:/#/user/liuna?_k=adseisnginx

使用browserHistory,瀏覽器的url是這樣的:/user/liunaexpress

這樣看起來固然是browerHistory更好一些,可是它須要server端支持。瀏覽器

使用hashHistory時,由於有 # 的存在,瀏覽器不會發送request,react-router 本身根據 url 去 render 相應的模塊。bash

使用browserHistory時,從 / 到 /user/liuna, 瀏覽器會向server發送request,因此server要作特殊請求,好比用的 express 的話,你須要 handle 全部的路由 app.get('*', (req, res) => { ... }),使用了 nginx 的話,nginx也要作相應的配置。讓服務端都重定向到index.html頁面react-router

 

若是隻是靜態頁面,就不須要用browserHistory,直接hashHistory就行了。app

相關文章
相關標籤/搜索