UMI antd学习-10 Dva 与后台数据交互

 明天在线办公没时间学习了,今天晚上再更新一篇html

代码提交一次:https://github.com/zhaogaojian/jgdemoios

一、src下建立services目录git

建立文件userSrv.tsgithub

export async function query(): Promise<any> {
    return request('user/getuserinfo');
  }

import axios from "axios"
export default async function request (options) {
    let response
    try {
        response = await axios(options)
        return response
    } catch (err) {
        return response
    }
}

注意若是未安装axios,请安装axiosaxios

yarn add axios

 

 

 三、User.tsx增长一个按钮async

<Button type="primary" style={{marginTop:10,width:300}} onClick={this.handleReadFromSrvClick}>从服务端读取</Button>

 

 

 四、事件,不须要传参数的话,payload能够不填内容学习

 handleReadFromSrvClick = e => {
    this.props.dispatch({
      type:"User/getUserInfo",
      payload:null
    })
    console.log('click: ', e);
  };

五、models目录下的 user.ts中增长以下代码this

import {  query as queryUser } from '@/services/userSrv';

注意:下面effects不须要再使用queryUser.queryspa

 effects:
    {
        *getUserInfo(_, { call, put }) {
            const response = yield call(queryUser);
            //yield console.log(response.data);
            yield put({
              type: 'User/updateUserState',
              payload: response.data,
            });
          }
        
    }

 

 

 六、mock 增长user.ts3d

export default {
    '/user/getuserinfo': {UserInfo:{
        username:"李四"
    }}
  };
  

七、运行效果

 

 点击从服务端读取,姓名变成了李四,达到预期效果

 八、不分层与后台数据交互方法请参考

https://www.cnblogs.com/zhaogaojian/p/12238299.html

相关文章
相关标签/搜索