react-router

2017-05-24  本文已影响0人  游荡的猫咪
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>
上一篇 下一篇

猜你喜欢

热点阅读