## 數據流準備
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