|
redux-thunk[使用这个可以让action 成为一个函数] 中间件 发送ajax请求
1. 首先明白这是 redux 的中间件 ;
2. 安装且在 store 中引入 。
3. 然后再 actionCreators.js 中多添加一个action 且该action 返回的是个函数
export const getTodoList = (){
// 返回的函数会自动接收到 dispatch 对象
return (dispatch) =>{
axios.get('/list.json').then((res)=>{
let data = res.data;
// 这个lmChangeInpValue 就是该action 文件中的一个
let action = lmChangeInpValue(data);
// 派发action
dispatch(action);
})
}
}
4. 在模块中:
lmfun (){
const action = getTodolist();
store.dispatch(action);
}
5. 总结:
如果把axios 写在生命周期函数中,会使得模块变得越来越大,且不容易管理,
redux-thunk 主要是 为了 把ajax 转到 actionCreators.js 中
|
|