react + react-router + redux + ant-Desgin 搭建react管理后台 — 路由搭建(四)

释放双眼,带上耳机,听听看~!

前言

**** 学习总结使用,博客如中有错误的地方,请指正。改系列文章主要记录了搭建一个管后台的步骤,主要实现的功能有:使用路由模拟登录、退出、以及切换不同的页面;使用redux实现面包屑;引入使用其他常用的组件,比如highchart、富文本等,后续会继续完善。

github地址:https://github.com/huangtao5921/react-antDesgin-admin (欢迎Star)

**项目展示地址:https://huangtao5921.github.io/react-admin/ **

** **

一、安装react-router-dom

上一篇文章中 react + react-router + redux + ant-Desgin 搭建react管理后台 — 引入ant-Desgin并测试(三)我们已经将文件添加并成功引入了ant-Design。

接下来我们要开始将项目的路由搭建出来,这里我们使用的是React-router V4。

首先,执行如下命令安装
** react-router-dom**,主要用到
react-router-dom 中的 **Route, Switch, Redirect。**这里会涉及到 react-router 与 react-router-dom的区别,其实react-router实现了路由的核心功能,而react-router-dom基于react-router,且添加了一些DOM类的组件,比如<Link><BrowserRouter><Switch>等,我们只需要引入react-router-dom即可。


1
2
3
1$ yarn add react-router-dom --save-dev
2
3

安装成功后,我们改写一下每个页面的代码,并将组件导出,比如:首页的home.js改成如下,其他页面类似


1
2
3
4
5
6
7
8
9
10
11
12
1import React from &#x27;react&#x27;;
2class Home extends React.Component {
3  render() {
4    return (
5      &lt;div&gt;首页&lt;/div&gt;
6    );
7  }
8}
9
10export default Home
11
12

 

二、配置路由

改写我们的代码,这里改写的思路是:需要登录才能看到的页面的路由配置,我们全部放到routes/index.js中,我取名叫做Main,登录页以及Main的路由我们放在App.js中。index.js

中需要引入** BrowserRouter。**具体改写如下:

routes/index.js如下,写好之后在根目录下的App.js引入


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1import React from &#x27;react&#x27;;
2import { Route, Switch } from &#x27;react-router-dom&#x27;;
3import Home from &#x27;../pages/home/Home&#x27;;
4import Connect from &#x27;../pages/user/connect/Connect&#x27;;
5import List from &#x27;../pages/user/list/List&#x27;;
6import Rich from &#x27;../pages/tool/rich/Rich&#x27;;
7import NotFind from &#x27;../pages/notFind/NotFind&#x27;;
8
9class Index extends React.Component{
10  render() {
11    return (
12      &lt;Switch&gt;
13        &lt;Route exact path=&quot;/&quot; component={ Home }/&gt;
14        &lt;Route path=&quot;/user/connect&quot; component={ Connect }/&gt;
15        &lt;Route path=&quot;/user/list&quot; component={ List }/&gt;
16        &lt;Route path=&quot;/tool/rich&quot; component={ Rich }/&gt;
17        &lt;Route component={ NotFind }/&gt;
18      &lt;/Switch&gt;
19    );
20  }
21}
22
23export default Index;
24
25

根目录下的App.js如下,写好之后,在根目录的index.js中引入


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1import React from &#x27;react&#x27;;
2import { Route, Switch, Redirect } from &#x27;react-router-dom&#x27;;
3import &#x27;./app.css&#x27;;
4import Login from &#x27;./pages/login/Login&#x27;;
5import Main from &#x27;./routes/Main&#x27;;
6
7class App extends React.Component {
8  render() {
9    return (
10      &lt;div className=&quot;App&quot;&gt;
11        &lt;Switch&gt;
12          &lt;Route path=&quot;/login&quot; component={ Login } /&gt;
13          &lt;Route path=&quot;/&quot; component={ Main }/&gt;
14          &lt;Redirect to=&quot;/&quot;/&gt;
15        &lt;/Switch&gt;
16      &lt;/div&gt;
17    );
18  }
19}
20
21export default App;
22

根目录下的index.js如下:


1
2
3
4
5
6
7
8
9
10
11
1import React from &#x27;react&#x27;;
2import ReactDOM from &#x27;react-dom&#x27;;
3import App from &#x27;./App&#x27;;
4import { BrowserRouter } from &#x27;react-router-dom&#x27;;
5
6ReactDOM.render(
7  &lt;BrowserRouter&gt;
8    &lt;App/&gt;
9  &lt;/BrowserRouter&gt;
10  , document.getElementById(&#x27;root&#x27;));
11

当上面的路由都处理好之后,运行我们的项目,此时在3000端口打开,http://localhost:3000/,路由命中了我们的首页,页面展示的就是“首页”2个字。我们再尝试在浏览器中输入其他的一些路由,看看是否都成功,如果跟我下面的图展示的是一样的话,说明路由配置已经成功了,现在项目能根据不同的url访问到我们不同的页面了。

比如:http://localhost:3000/login访问的是登录页

react + react-router + redux + ant-Desgin 搭建react管理后台 -- 路由搭建(四)     

 

http://localhost:3000/user/list访问的是用户列表页,其他的页面同理

给TA打赏
共{{data.count}}人
人已打赏
安全技术

哈希(Hash)与加密(Encrypt)的基本原理、区别及工程应用

2021-8-18 16:36:11

安全技术

C++ 高性能服务器网络框架设计细节

2022-1-11 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索