上篇讲解了redux-thunk中间件的使用,本篇文章介绍一下另一种实现异步请求的的方式,也是使用中间件实现的,但方法略有不同,其他模块一样,只是在actionCreator中请求数据不同,先在store中引入redux-promise-middleware,然后使用中间件。这里只上actionCreator中的代码:
import {fetch as fetchpro} from "whatwg-fetch";
//与react-thunk不同的是这里action是一个对象
export const MovieAction = ()=>({
type:"GET_MOVIE",
payload:new Promise(resolve=>{
//异步
fetchpro("/api/ajax/movieOnInfoList?token=")
.then(res=>res.json())
.then((data)=>{
resolve(data);
})
})
})