本文主要和大家介绍react router 4.0以上的路由应用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能帮助到大家。
在4.0以下的react router中,嵌套的路由可以放在一个router标签中,形式如下,嵌套的路由也直接放在一起。
<Route component={App}>
<Route path="groups" components={Groups} />
<Route path="users" components={Users}>
<Route path="users/:userId" component={Profile} />
</Route>
</Route>但是在4.0以后,嵌套的路由与之前的就完全不同了,需要单独放置在嵌套的根component中去处理路由,否则会一直有warning:
You should not use
正确形式如下
<Route component={App}>
<Route path="groups" components={Groups} />
<Route path="users" components={Users}>
//<Route path="users/:userId" component={Profile} />
</Route>
</Route>上面将嵌套的路由注释掉
const Users = ({ match }) => (
<p>
<h2>Topics</h2>
<Route path={`${match.url}/:userId`} component={Profile}/>
</p>
)上面在需要嵌套路由的component中添加新的路由
一个完整的嵌套路由的例子如下
51shop 由 PHP 语言开发, 使用快速的 MySQL 数据库保存数据 ,为中小型网站实现网上电子商务提供一个完美的解决方案.一、用户模块1. 用户注册:用户信息包括:用户ID、用户名、用户密码、性别、邮箱、省份、城市、 联系电话等信息,用户注册后不能立即使用,需由管理员激活账号,才可使用(此功能管理员可设置)2. 登录功能3. 资料修改:用户可修改除账号以后的所有资料4. 忘记密码:要求用
0
说明及注意事项
1.以下代码采用ES6格式
2.react-router-dom版本为4.1.1
3.请注意使用诸如HashRouter之类的history,否则一直会有warning,不能渲染
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
// import { Router, Route, Link, Switch } from 'react-router';
import {
HashRouter,
Route,
Link,
Switch
} from 'react-router-dom';
class App extends Component {
render() {
return (
<p>
<h1>App</h1>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/inbox">Inbox</Link></li>
</ul>
{this.props.children}
</p>
);
}
}
const About = () => (
<p>
<h3>About</h3>
</p>
)
const Home = () => (
<p>
<h3>Home</h3>
</p>
)
const Message = ({ match }) => (
<p>
<h3>new messages</h3>
<h3>{match.params.id}</h3>
</p>
)
const Inbox = ({ match }) => (
<p>
<h2>Topics</h2>
<Route path={`${match.url}/messages/:id`} component={Message}/>
</p>
)
ReactDOM.render(
(<HashRouter>
<App>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/inbox" component={Inbox} />
</App>
</HashRouter>),
document.getElementById('root')
);相关推荐:
react-router4 配合webpack require.ensure 实现异步加载
以上就是react router4.0以上的路由使用方法的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号