[TOC]
# 安装依赖:
先安装依赖 'dva-core','dva-loading';
# dva.js
`src`目录下,`dva.js`内容:
```js
import Taro from '@tarojs/taro';
import { create } from 'dva-core';
import createLoading from 'dva-loading';
// import {createLogger} from 'redux-logger';
let app;
let store;
let dispatch;
function createApp(opt) {
// redux日志
// opt.onAction = [createLogger()];
app = create(opt);
app.use(createLoading({}));
// 适配支付宝小程序
if (Taro.getEnv() === Taro.ENV_TYPE.ALIPAY) {
global = {};
}
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;
},
};
```
# 启动创建 store
在启动页,如`app.js` 内:
```
const dvaApp = dva.createApp({
initialState: {},
models: models,
onError(e, dispatch) {
// dispatch(action('sys/error', e));
},
});
const store = dvaApp.getStore();
...
render() {
return (
<Provider store={store}>
<Index />
</Provider>
);
}
```
# 参考
[taro-dva-master](https://github.com/chenfangsheng/taro-dva-master)