四步走
- 創(chuàng)建根節(jié)點(diǎn) <BroswerRouter /> 或者 <HashRouter />碍侦,分別代表
history
模式和hash
模式 - 創(chuàng)建路由組 <Routes />
- 創(chuàng)建路由 <Route />,綁定path和element
- 創(chuàng)建鏈接按鈕 <Link />,綁定點(diǎn)擊去到的路徑to
hook
-
useLocation
最常用的,一個(gè)對(duì)象,包括路徑和路由
原理
- 組件切換:獲取當(dāng)前l(fā)ocation雷猪,返回不同組件
- hash:onHashChange() 獲取路徑更新,window.location.hash 改變路徑
- history:訂閱history獲取路徑更新晰房,window.history.pushState() 改變路徑