Skip to content

Commit 239446d

Browse files
committed
config: 🔧 新增路由管理react-router-dom v6
1 parent d426477 commit 239446d

23 files changed

+298
-60
lines changed

README.md

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
<h1 align="center">react 模板</h1>
22
<br/>
33

4-
54
## 👨🏻‍💻 项目说明
5+
66
react 模板
77

88
## 🚀 技术栈
99

10-
- React
11-
- react-dom
10+
- React v18
11+
- react-dom v18
1212
- React Hook
13-
- TypeScript
14-
- webpack
13+
- TypeScript v4
14+
- webpack v5
1515
- axios
1616
- mobx-react-lite
17-
- react-router-dom
18-
- react-router-config
17+
- react-router-dom v6
1918
- postcss-px-to-viewport
2019

21-
2220
## ⌛️ 安装项目依赖
2321

2422
```
@@ -48,18 +46,20 @@ npm run build:prod // 线上环境
4846

4947
## 💡 分支说明
5048

51-
| 分支 | 说明 |
52-
| -- | -- |
53-
| main | 主分支 |
54-
| dev | 开发分支 |
49+
| 分支 | 说明 |
50+
| ---- | -------- |
51+
| main | 主分支 |
52+
| dev | 开发分支 |
5553

5654
## 代码提交规范
55+
5756
```
5857
git <type>: <subject>
5958
git commit -m “feat: 项目初始化”
6059
```
6160

62-
### type参考:
61+
### type 参考:
62+
6363
```
6464
fix 🐛 Bug修复
6565
feature ✨ 引入新特性
@@ -80,15 +80,15 @@ merge 🔀 合并分支
8080
## 📂 目录结构
8181

8282
```
83-
├── .vscode
83+
├── .vscode
8484
│ └──setting.json # 先于vscode全局的settings.json配置
8585
├── doc # 开发文档记录
8686
├── webpack # 打包编译
8787
│ └──config # webpack配置
8888
│ ├── webpack.common.js # 基础配置
8989
│ ├── webpack.dev.js # 开发环境配置
9090
│ └──webpack.prod.js # 生产环境配置
91-
├── pubilc
91+
├── pubilc
9292
│ ├──favicon.ico # HTML图标
9393
│ └──index.html # HTML入口模板
9494
├── src
@@ -107,7 +107,7 @@ merge 🔀 合并分支
107107
├── .babelrc.js # babel配置
108108
├── .env.json # 环境变量配置
109109
├── .gitignore # git提交忽略文件
110-
├── .npmrc
110+
├── .npmrc
111111
├── .prettierignore # prettierc忽略文件
112112
├── .prettierrc # prettierc配置
113113
├── .eslintrc.js # ESLint配置
@@ -118,5 +118,6 @@ merge 🔀 合并分支
118118
```
119119

120120
## 技术栈说明
121-
- React18
122-
- TypeScript
121+
122+
- React18
123+
- TypeScript

package-lock.json

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
},
108108
"dependencies": {
109109
"react": "^18.2.0",
110-
"react-dom": "^18.2.0"
110+
"react-dom": "^18.2.0",
111+
"react-router-dom": "^6.8.1"
111112
}
112113
}

src/App.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import React from 'react';
2-
import Home from '@/view/Home';
2+
import {useRoutes} from 'react-router-dom';
3+
import Tab from '@/view/Tab';
4+
import routes from '@/router';
35
import '@/styles/index.scss';
46

57
function App() {
8+
// 通过useRoutes配置实现路由管理
9+
const element = useRoutes(routes);
610
return (
711
<div className='app'>
8-
<Home name='React App' />
12+
<Tab />
13+
{element}
914
</div>
1015
);
1116
}

src/components/Suspenselazy.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React, {Suspense, lazy} from 'react';
2+
3+
const Suspenselazy = (props: any) => {
4+
return <Suspense fallback={<>...</>}>{React.createElement(lazy(props))}</Suspense>;
5+
};
6+
7+
export default Suspenselazy;

src/index.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom/client';
3+
import {BrowserRouter} from 'react-router-dom';
34
import App from './App';
45

56
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLDivElement);
6-
7-
root.render(<App />);
7+
root.render(
8+
<BrowserRouter>
9+
<App />
10+
</BrowserRouter>
11+
);

src/router/index.tsx

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* react-router-dom v6 官方文档
3+
* https://reactrouter.com/en/v6.3.0/getting-started/installation
4+
*/
5+
import React from 'react';
6+
import Suspenselazy from '@/components/Suspenselazy';
7+
import {Navigate, RouteObject} from 'react-router-dom';
8+
9+
const Home = Suspenselazy(() => import(/* webpackChunkName:"" */ '@/view/Home'));
10+
const HomeOne = Suspenselazy(() => import(/* webpackChunkName:"homeOne" */ '@/view/Home/HomeOne'));
11+
const HomeTwo = Suspenselazy(() => import(/* webpackChunkName:"homeTwo" */ '@/view/Home/HomeTwo'));
12+
const Dashboard = Suspenselazy(() => import(/* webpackChunkName:"dashboard" */ '@/view/Dashboard'));
13+
const About = Suspenselazy(() => import(/* webpackChunkName:"about" */ '@/view/About'));
14+
15+
const routes: RouteObject[] = [
16+
{
17+
path: '/',
18+
element: <Navigate to='/home/one' /> // 重定向
19+
},
20+
{
21+
path: '/home',
22+
element: Home,
23+
children: [
24+
// 嵌套路由
25+
{
26+
path: '/home/one',
27+
element: HomeOne
28+
},
29+
{
30+
path: '/home/two',
31+
element: HomeTwo
32+
}
33+
]
34+
},
35+
{
36+
path: '/dashboard',
37+
element: Dashboard
38+
},
39+
{
40+
path: '/about',
41+
element: About
42+
}
43+
];
44+
45+
export default routes;

src/styles/index.scss

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
* {
2+
margin: 0;
3+
padding: 0;
4+
}
5+
16
#root {
27
.app {
3-
height: 100vh;
4-
background: #cccccc;
5-
position: fixed;
6-
top: 0;
7-
right: 0;
8-
bottom: 0;
9-
left: 0;
8+
background: #e2e7e033;
109
}
1110
}

src/view/About/index.scss

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.about-root {
2+
display: flex;
3+
align-items: center;
4+
justify-content: center;
5+
width: 100vw;
6+
height: 100vh;
7+
padding-top: 70px;
8+
font-size: 50px;
9+
background: #30afe0;
10+
}

src/view/About/index.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react';
2+
import './index.scss';
3+
4+
const About = () => {
5+
return (
6+
<div className='about-root'>
7+
<p>Hello About</p>
8+
</div>
9+
);
10+
};
11+
12+
export default About;

src/view/Dashboard/index.scss

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.dashboard-root {
2+
& > span {
3+
display: block;
4+
width: 100%;
5+
font-weight: 800;
6+
font-size: 110px;
7+
}
8+
}

src/view/Dashboard/index.tsx

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react';
2+
import './index.scss';
3+
4+
const Dashboard = () => {
5+
return (
6+
<div className='dashboard-root'>
7+
<span>hello world Dashboard </span>
8+
<span>hello world Dashboard </span>
9+
<span>hello world Dashboard </span>
10+
<span>hello world Dashboard </span>
11+
<span>hello world Dashboard </span>
12+
<span>hello world Dashboard </span>
13+
<span>hello world Dashboard </span>
14+
<span>hello world Dashboard </span>
15+
<span>hello world Dashboard </span>
16+
<span>hello world Dashboard </span>
17+
<span>hello world Dashboard </span>
18+
<span>hello world Dashboard </span>
19+
</div>
20+
);
21+
};
22+
23+
export default Dashboard;

src/view/Home.tsx

-17
This file was deleted.

src/view/Home/HomeOne/index.scss

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.home-one-root {
2+
display: flex;
3+
align-items: center;
4+
justify-content: center;
5+
box-sizing: border-box;
6+
width: 1000px;
7+
height: 800px;
8+
font-size: 50px;
9+
background: #c6e2ff;
10+
border-radius: 10px;
11+
}

src/view/Home/HomeOne/index.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
import './index.scss';
3+
4+
const HomeOne = () => {
5+
return <div className='home-one-root'>HomeOne</div>;
6+
};
7+
8+
export default HomeOne;

src/view/Home/HomeTwo/index.scss

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.home-two-root {
2+
display: flex;
3+
align-items: center;
4+
justify-content: center;
5+
box-sizing: border-box;
6+
width: 1000px;
7+
height: 800px;
8+
font-size: 50px;
9+
background: #ffe7ba;
10+
border-radius: 10px;
11+
}

src/view/Home/HomeTwo/index.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
import './index.scss';
3+
4+
const HomeTwo = () => {
5+
return <div className='home-two-root'>HomeTwo</div>;
6+
};
7+
8+
export default HomeTwo;

src/view/Home/index.scss

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.home-root {
2+
display: flex;
3+
width: 100vw;
4+
height: 100vh;
5+
padding-top: 70px;
6+
7+
.home-tab {
8+
display: flex;
9+
flex-direction: column;
10+
box-sizing: border-box;
11+
width: 300px;
12+
height: 800px;
13+
padding: 10px;
14+
font-size: 25px;
15+
background: #00bfff;
16+
border-radius: 10px;
17+
}
18+
}

0 commit comments

Comments
 (0)