10分钟搞定react-router

本文详细介绍 React Router 的安装、配置及使用方法,包括平级路由与嵌套路由的应用实例,并通过具体代码展示了如何实现页面间的跳转。

1.路由的安装:

$ npm install -S react-router


2.引入路由文件

import {Router, Route, browserHistory} from 'react-router';


3.配置路由器

  平级路由(做跳转用)

const router = (
<Router history={browserHistory}>
<Route path="/" component={App}/>
<Route path="about" component={About}/>
<Route path="concat" component={Concat}/>
<Route path="list/:id" component={List}/>
</Router>
);
// Render the main component into the dom
ReactDOM.render(router, document.getElementById('app'));

  嵌套路由(做嵌套)

const router = (
<Router history={browserHistory}>
<Route path="/" component={App}>
<Route path="about" component={About}/>
<Route path="concat" component={Concat}/>
<Route path="list/:id" component={List}/>
</Route>
</Router>
);

 

 

 

 

应用:

class App extends React.Component {
render() {
return (
<div>
<h1>React Router Demo</h1>
<hr />
<p>
by <a href="http://stylechen.com/" target="_blank">stylechen.com</a>
</p>
<Link to="/">Home</Link>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/concat">Concat</Link></li>
<li><Link to="/list/001">List 001</Link></li>
<li><Link to="/list/002">List 002</Link></li>
</ul>
</div>

);
}
}

转载于:https://www.cnblogs.com/cshi/p/6121123.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值