首先我们需要安装axios
npm install --save axios
在安装完成之后,假设在页面加载的时候就去请求数据
在easy-mock上面模拟的请求数据
componentDidMount(){
Axios.get('https://www.easy-mock.com/mock/5f5f59a83d43414305879b1c/example/mock').then((res)=>{
const action={
type:GET_LIST,
data:res.data.data.data
}
console.log(res)
store.dispatch(action)
})
}
在reducer文件里面处理传递的数据
if(action.type===GET_LIST){
let newState=JSON.parse(JSON.stringify(state))
newState.data=action.data
console.log(newState)
return newState //在数据传递的最后,记得一定要进行返回
}