首页 > 其他分享 >Ant Desing Pro与服务端交互

Ant Desing Pro与服务端交互

时间:2023-02-25 18:12:57浏览次数:56  
标签:const title Pro register Ant respose import data 服务端

参考资料:

https://segmentfault.com/a/1190000018422882
https://www.yuque.com/ant-design/course/ig6mzb

 

/services/ant-design-pro/register.js

import { CONFIG } from '@/consts/config';
import { request } from 'umi';

export async function register() {
  return request(`${CONFIG.URL}/HTRegister`, {
    method: 'POST',
  });
}

export async function register_select() {
  return request(`${CONFIG.URL}/HTRegister_select`, {
    method: 'POST',
  });
}

 

/models/register.js

import { register } from '@/services/ant-design-pro/register';
import { register_select } from '@/services/ant-design-pro/register';

export default {
  namespace: 'register',

  state: {
    data: [],
  },

  effects: {
    *queryInitCards(_, sagaEffects) {
      const { call, put } = sagaEffects;

      const respose = yield call(register);
      console.log('请求的返回值test:' + JSON.stringify(respose.info));
      yield put({ type: 'save', payload: respose.info });
    },

    *queryInitCards_s(_, sagaEffects) {
      const { call, put } = sagaEffects;

      const respose = yield call(register_select);
      console.log('请求的返回值test:' + JSON.stringify(respose.info));
      yield put({ type: 'save', payload: respose.info });
    },
  },

  reducers: {
    save(state, action) {
      console.log('处理后的test:' + action.payload);
      return {
        ...state, // es6三点运算符合,有点模糊解释不清楚
        data: action.payload, // 上面与服务器交互完的数据赋值给data,这里的data 对应最上面 state 里面的data
      };
    },
  },
};
  • 添加了一个 effect queryInitCards
  • const respose = yield call(register);获取服务端数据。
  • yield put({ type: 'save', payload: respose.info });添加一个卡片数据。这个会触发 reducer 的执行。于是会看到视图上添加了一个新卡片。

 

pages/register.jsx

import { Button } from 'antd';
import { Divider, List, Typography } from 'antd';
import { Card } from 'antd';
import { Space, Table, Tag } from 'antd';
import React from 'react';
import { render } from 'react-dom';
import { connect } from 'dva';
import { register } from '@/services/ant-design-pro/register';

const namespace = 'register';

const mapStateToProps = (state) => {
  const userList = state[namespace].data;
  // const userListtoJson = JSON.stringify(userList);
  console.log('给page页的数据test' + JSON.stringify(userList));
  //console.log('给page页的数据' + userListtoJson['data']['email']);
  return {
    userList,
  };
};

const mapDispatchToProps = (dispatch) => {
  return {
    onDidMount: () => {
      dispatch({
        type: `${namespace}/queryInitCards`,
      });
    },

    onDidMount_select: () => {
      dispatch({
        type: `${namespace}/queryInitCards_s`,
      });
    },
  };
};

const columns = [
  {
    title: 'Email',
    dataIndex: 'email',
    key: 'email',
    render: (text) => <a>{text}</a>,
  },
  {
    title: 'user_id',
    dataIndex: 'user_id',
    key: 'user_id',
  },
  {
    title: '国籍',
    dataIndex: '国籍',
    key: '国籍',
  },

  {
    title: '学习语言',
    dataIndex: '学习语言',
    key: '学习语言',
  },

  {
    title: '性别',
    dataIndex: '性别',
    key: '性别',
  },

  {
    title: '母语',
    dataIndex: '母语',
    key: '母语',
  },

];

@connect(mapStateToProps, mapDispatchToProps)
class ShoppingList extends React.Component {
  render() {
    console.log('展示的数据test' + JSON.stringify(this.props.userList));
    return (
      <>
        <Card>
          <Button type="primary" onClick={this.props.onDidMount}>
            注册
          </Button>
          <Button type="primary" onClick={this.props.onDidMount_select}>
            查询
          </Button>
        </Card>

        <Table columns={columns} dataSource={this.props.userList} />
      </>
    );
  }
}

export default ShoppingList;
  • 通过 mapDispatchToProps 给页面注入方法 onDidMount。页面在 mount 完毕后调用该方法。它发送一个 puzzlecards/queryInitCards 的 action

标签:const,title,Pro,register,Ant,respose,import,data,服务端
From: https://www.cnblogs.com/QingshanY/p/17154928.html

相关文章

  • Ant Design Pro 请求地址
    修改后端地址\src\consts\config.jsexportconstCONFIG={URL:'http://localhost:7777',ROLE:{0:'user',1:'admin',2:'superAdmin'}}......
  • Ant Design Pro 使用model
    引入DVAexportdefault{plugins:[['umi-plugin-react',{antd:true,dva:true,}],],//...} 新建src/models/register.jsexp......
  • Ant Design Pro 新建页面
    新建src/pages/Request.jsimportReactfrom'react';exportdefault()=>{return(<div>临时用</div>);} 修改config/routes.jspath:路径compone......
  • Ant Design Pro 安装
    使用npm安装cnpmnpminstall-gcnpm--registry=https://registry.npm.taobao.org 安装yarncnpminstall-gyarn 安装Umicnpminstall-gumi 安装项......
  • 如何修改Cognex VisionPro中Blob斑点的边界颜色
    截取项目中的一段代码说明:stringstr="";intCount=0;for(inti=0;i<ParticleCount;i++){doubleCoM_X=particleblob.Re......
  • Android笔记--FileProvider
    FileProvider介绍继承于ContentProvider,本质上依旧是用于跨境通信,对第三方应用暴露文件,并授予文件读写地权限具体内容1、在Strings.xml里面配置一个常量2、在Manifest......
  • What's past is prologue
        凡是过去,皆为序章。爱所有人,信任少数人,不负任何人。我荒废了时间,时间便把我荒废了。 在灰暗的日子中,不要让冷酷的命运窃喜;命运既然来凌辱我们,就应该用处之泰......
  • 使用docker-compose快速部署Prometheus+grafana环境
    由于最近公司服务频繁出问题,老板很生气,下面的人都很不好过,于是老大让加一下业务监控,来观察线上数据状态。但是由于qa环境数据量太少,所以自己搭建了一套环境做相关监控,并且......
  • 性能测试-grafana + prometheus + node_exporter
    1、grafana安装下载网址:https://grafana.com/grafana/download/7.4.3?platform=linux#下载wgethttps://dl.grafana.com/enterprise/release/grafana-enterprise-7.4.3......
  • Docker 启动报:Job for docker.service failed because the control process exited wi
    方法:1、在/etc/docker/daemon.json路径下添加代码。没有这个文件的可以手动添加touch/etc/docker/daemon.json在里面添加代码:{"storage-driver":"devicemapper"}......