import axios from 'axios';
export default {
namespace: 'itemData',
state: [],
reducers: {
addList(state, { payload }) {
let newArray = [];
for (let i = 0; i < payload.length; i++) {
let e = payload[i];
if (Object.keys(e).length == 0) continue;
newArray.push({
id: e.id,
start: e.start,
score: e.score,
author: e.author,
});
}
return [...state, ...newArray];
},
},
effects: {
* search(action, { call, put }) {
let res = yield call(axios.get, 'http://localhost:8080/api/getItem');
yield put({ type: 'addList', payload: res.data });
},
},
subscriptions: {
setup({ dispatch }) {
document.addEventListener('readystatechange', e => {
if (document.readyState == "complete"){
dispatch({
type: 'search'
});
}
});
},
},
};
dva 页面初次加载时读取数据
最新推荐文章于 2020-12-14 22:45:45 发布