react-router安装命令
npm install -S react-router
react-router的使用
import { Router, Route, hashHistory } from 'react-router';
render((
<Router history={hashHistory}>
<Route path="/" component={App}/>
</Router>
), document.getElementById('app'));
Router组件有一个参数history,它的值hashHistory表示,路由的切换由URL的hash变化决定
嵌套路由
<Router history={hashHistory}>
<Route path="/" component={App}>
<Route path="/repos" component={Repos}/>
<Route path="/about" component={About}/>
</Route>
</Router>
用户访问repos组件的时候会先加载app再加载repos
IndexRoute
默认情况下加载的组件
<Router>
<Route path="/" component={App}>
<IndexRoute component={Home}/>
<Route path="accounts" component={Accounts}/>
<Route path="statements" component={Statements}/>
</Route>
</Router>