Skip to content

Commit 5f7483f

Browse files
author
Sven
committed
refactor(routes): 路由集中在routes/index 配置
1 parent 7e2fcda commit 5f7483f

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/layouts/PageLayout/PageLayout.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react'
2-
import { NavLink as Link, Switch, Route, Redirect } from 'react-router-dom'
2+
import { NavLink as Link } from 'react-router-dom'
33
import { Collapse, Navbar, NavbarToggler, NavbarBrand, Nav, NavLink, Container } from 'reactstrap'
44
import PropTypes from 'prop-types'
55
import './PageLayout.scss'
6-
import Home, { AsyncCounter, AsyncZen, AsyncElapse, AsyncRoute, AsyncPageNotFound } from '../../routes'
6+
import Routes from '../../routes'
77

88
const location = {
99
pathname : '/route/8080',
@@ -50,15 +50,7 @@ export default class PageLayout extends React.Component {
5050
</Collapse>
5151
</Navbar>
5252
<Container className='text-center page-layout__viewport'>
53-
<Switch>
54-
<Route exact path='/' component={Home} />
55-
<Route path='/counter' component={AsyncCounter} />
56-
<Route path='/zen' component={AsyncZen} />
57-
<Route path='/elapse' component={AsyncElapse} />
58-
<Route path='/route/:id' component={AsyncRoute} />
59-
<Route path='/404' component={AsyncPageNotFound} />
60-
<Redirect from='*' to='/404' />
61-
</Switch>
53+
<Routes />
6254
</Container>
6355
</div>
6456
)

src/routes/index.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
import asyncComponent from '../components/AsyncComponent'
22
import Home from './Home'
3+
import { Switch, Route, Redirect } from 'react-router-dom'
4+
import React from 'react'
35

4-
export default Home
5-
export const AsyncCounter = asyncComponent(() => import(
6+
const AsyncCounter = asyncComponent(() => import(
67
/* webpackChunkName: "counter" */
78
'./Counter'))
8-
export const AsyncZen = asyncComponent(() => import(
9+
const AsyncZen = asyncComponent(() => import(
910
/* webpackChunkName: "zen" */
1011
'./Zen'))
11-
export const AsyncElapse = asyncComponent(() => import(
12+
const AsyncElapse = asyncComponent(() => import(
1213
/* webpackChunkName: "elapse" */
1314
'./Elapse'))
14-
export const AsyncRoute = asyncComponent(() => import(
15+
const AsyncRoute = asyncComponent(() => import(
1516
/* webpackChunkName: "route" */
1617
'./Route'))
17-
export const AsyncPageNotFound = asyncComponent(() => import(
18+
const AsyncPageNotFound = asyncComponent(() => import(
1819
/* webpackChunkName: "pageNotFound" */
1920
'./PageNotFound'))
21+
22+
const Routes = () => (
23+
<Switch>
24+
<Route exact path='/' component={Home} />
25+
<Route path='/counter' component={AsyncCounter} />
26+
<Route path='/zen' component={AsyncZen} />
27+
<Route path='/elapse' component={AsyncElapse} />
28+
<Route path='/route/:id' component={AsyncRoute} />
29+
<Route path='/404' component={AsyncPageNotFound} />
30+
<Redirect from='*' to='/404' />
31+
</Switch>
32+
)
33+
34+
export default Routes

0 commit comments

Comments
 (0)