Fork me on GitHub

dva的按需加载路由以及对应的model

问题

最近项目中用到dva,因项目属于webview方面,只是内嵌在app里的一些零散的页面,页面之间并没有关联,因此考虑到dva的按需加载路由以及对应的组件

解决方案

使用dynamic解决相关问题:
router.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import React from 'react';
import { Router, Route, Switch } from 'dva/router';
import dynamic from "dva/dynamic";

function RouterConfig({ history, app }) {

const Summary = dynamic({
app,
component: () => import('./routes/summary/summary')
});

const AgreementListPage = dynamic({
app,
component: () => import('./routes/Agreement/ListPage')
});

const Agreement1Page = dynamic({
app,
component: () => import('./routes/Agreement/agreement1Page')
});

const Agreement2Page = dynamic({
app,
component: () => import('./routes/Agreement/agreement2Page')
});

const Agreement3Page = dynamic({
app,
component: () => import('./routes/Agreement/agreement2Page')
});

const BusinessLoans = dynamic({
app,
models: () => [import('./models/businessLoans')], //动态引入model,可以引入多个model
component: () => import('./routes/businessLoans/businessLoans') //动态引入组件
});


return (
<Router history={history}>
<Switch>
<Route path="/" exact component={Summary} />
<Route path="/agreement/list" exact component={AgreementListPage} />
<Route path="/agreement/agreement1" exact component={Agreement1Page} />
<Route path="/agreement/agreement2" exact component={Agreement2Page} />
<Route path="/agreement/agreement3" exact component={Agreement3Page} />
<Route path="/businessLoans" exact component={BusinessLoans} />
</Switch>
</Router>
);
}

export default RouterConfig;