[react-router] hashHistory 和 browserHistory 的區別


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也要做相應的配置。

 

如果只是靜態頁面,就不需要用browserHistory,直接hashHistory就好了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM