|
1 | 1 | import React from 'react';
|
2 |
| -import { Router } from 'dva/router'; |
3 |
| - |
4 |
| -const cached = {}; |
5 |
| -function registerModel(app, model) { |
6 |
| - if (!cached[model.namespace]) { |
7 |
| - app.model(model); |
8 |
| - cached[model.namespace] = 1; |
9 |
| - } |
10 |
| -} |
| 2 | +import { Router, Switch, Route } from 'dva/router'; |
| 3 | +import dynamic from 'dva/dynamic'; |
11 | 4 |
|
12 | 5 | function RouterConfig({ history, app }) {
|
13 |
| - const routes = [ |
14 |
| - { |
15 |
| - path: '/', |
16 |
| - name: 'IndexPage', |
17 |
| - getComponent(nextState, cb) { |
18 |
| - require.ensure([], (require) => { |
19 |
| - cb(null, require('./routes/IndexPage')); |
20 |
| - }); |
21 |
| - }, |
22 |
| - }, |
23 |
| - { |
24 |
| - path: '/users', |
25 |
| - name: 'UsersPage', |
26 |
| - getComponent(nextState, cb) { |
27 |
| - require.ensure([], (require) => { |
28 |
| - registerModel(app, require('./models/users')); |
29 |
| - cb(null, require('./routes/Users')); |
30 |
| - }); |
31 |
| - }, |
32 |
| - }, |
33 |
| - ]; |
| 6 | + const IndexPage = dynamic({ |
| 7 | + app, |
| 8 | + component: import('./routes/IndexPage'), |
| 9 | + }); |
| 10 | + |
| 11 | + const Users = dynamic({ |
| 12 | + app, |
| 13 | + models: [ |
| 14 | + import('./models/users'), |
| 15 | + ], |
| 16 | + component: import('./routes/Users'), |
| 17 | + }); |
34 | 18 |
|
35 |
| - return <Router history={history} routes={routes} />; |
| 19 | + return ( |
| 20 | + <Router history={history}> |
| 21 | + <Switch> |
| 22 | + <Route exact path="/" component={IndexPage} /> |
| 23 | + <Route exact path="/users" component={Users} /> |
| 24 | + </Switch> |
| 25 | + </Router> |
| 26 | + ); |
36 | 27 | }
|
37 | 28 |
|
38 | 29 | export default RouterConfig;
|
0 commit comments