0%

Taro学习-结合dva的使用

把Taro项目改造采用dva进行数据流管理

安装依赖

script
1
npm i dva-core dva-loading --save

创建文件 utils/dva.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
import {create} from 'dva-core';
import createLoading from 'dva-loading';

let app;
let store;
let dispatch;

function createApp(opt) {
app = create(opt);
app.use(createLoading({}));
if (!global.registered) opt.models.forEach(model => app.model(model));
global.registered = true;
app.start();
store = app._store;
app.getStore = () => store;
dispatch = store.dispatch;
app.dispatch = dispatch;
return app;
}

export default {
createApp,
getDispatch() {
return app.dispatch;
}
}

创建models文件夹放你逻辑代码

改造app.jsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import {Provider} from '@tarojs/redux'
import dva from './utils/dva'
import models from './model/index'

// dva 集成
const dvaApp = dva.createApp({
initialState: {},
models: models,
});
const store = dvaApp.getStore();
//包装render内容
...
render () {
return (
<Provider store={store}>
<Index />
</Provider>
)
}
...

其它的操作就和 dva+react 差不多的了