上一篇咱们说到了路由组件的嵌套。想必你已经运用自如了。那么,这一次咱们来聊一聊4.X中Router的变动。javascript
在3.X中咱们若使用路由的模式,可经过在Router上配置history的值便可。java
例如,react
import { Router, Route, hashHistory} from "react-router"
import routes from "./routes"
<Router history={hashHistory} routes={routes}>
<Route path="/" component={App}/>
</Router>
在4.X中提供几种不一样的路由组件来替代history属性的做用,分别是浏览器
<BrowserRouter>react-router
import { BrowserRouter } from 'react-router-dom' <BrowserRouter basename={optionalString} forceRefresh={optionalBool} getUserConfirmation={optionalFunc} keyLength={optionalNumber} > <App/> </BrowserRouter>
<HashRouter>dom
import { HashRouter } from 'react-router-dom' <HashRouter> <App/> </HashRouter>
<MemoryRouter> 通常用于测试或者无浏览器的环境中,像是react native中测试
<MemoryRouter initialEntries={[ '/one', '/two', { pathname: '/three' } ]} initialIndex={1} > <App/> </MemoryRouter>
<StaticRouter> 通常用于服务端,永远不会改变locationurl
<StaticRouter location={req.url} context={context}> <App/> </StaticRouter>
须要注意的是,router组件只能有一个子元素spa