## 数据流准备
1. 了解了dva数据流(基于redux二次封装)之后,我们来实战改造下之前写的模块
2. 到src/models下新建demo.js文件,内容如下
~~~
import { list } from '../services/demo';
export default {
namespace: 'demo',
state: {
data: {
list: [],
pagination: {},
},
detail: {},
},
effects: {
*fetchList({ payload }, { call, put }) {
const response = yield call(list, payload);
if (response.success) {
yield put({
type: 'saveList',
payload: {
list: response.data.records,
pagination: {
total: response.data.total,
current: response.data.current,
pageSize: response.data.size,
},
},
});
}
},
},
reducers: {
saveList(state, action) {
return {
...state,
data: action.payload,
};
},
},
};
~~~
3. 新建到model文件由四个部分组成,分别是:namespace、state、effects、reducers
4. namespace定义了model的命名空间,外部可通过对应的值检索到;
5. state是model内的状态,供外部调用和内部组织结构;
6. effects主要提供了异步操作,被dispatch函数调用,请求api并将返回数据交由reduceers处理并更新state;
7. reducers主要用于接受effects传递过来的数据并进行加工操作,最后更新到state中
## 注
effects中用到了es6的生成器函数,更多概念请移步:https://www.jianshu.com/p/e0778b004596