antd pro dva使用总结

1、在config/router.config.js配置自己要写的页面的路由,如果路由隐藏不显示在导航栏,设置hideInMenu为true 即可

2、在page文件夹下面新建test文件夹及里面的models文件夹

3、list.js中引入dva及connect

import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { Table, Card, Button } from 'antd';

@connect(({ user, list, loading }) => ({
  currentUser: user.currentUser,
  list
}))

class list extends PureComponent {
    constructor(props) {
        super(props);
        this.pageInfo = {
            showQuickJumper: true,
            showTotal(totalSize){
                return <div>符合条件共 <a style={{fontWeight: 'bold'}}>{totalSize}</a> 条</div>;
            }
        };
    }
    render(){
        const {
            list: { tableData, current, pageSize, totalSize },
        } = this.props;
        return(<Table columns={this.columns}
                        bordered
                        dataSource={tableData}
                        pagination={{ ...this.pageInfo, current: current, pageSize, total: totalSize }}
                        // loading={loading}
                        onChange={this.handleTableChange.bind(this)}/>
            );
    }
    componentDidMount() {
        this.getTableData();
    }
    getTableData(){
        const { dispatch } = this.props;
        //dispatch action models命名空间为list的下的getTableData
        dispatch({
            type: 'list/getTableData',
        });
    }

    handleTableChange(){

    }
    get columns(){
        return [
            {
              title: 'test',
              dataIndex: 'name',
            }
        ]
    }
}   
export default list;

modes/list.js内容如下:

import { tableurl } from '@/services/api';

export default {
  	namespace: 'list',

  	state: {
		tableData: [{test: '测试'}],
		totalSize: 40,
		current: 1,
		pageSize: 20
	
  	},

  	effects: {
		*fetch({ payload }, { call, put }) {
		  	const response = yield call(queryRule, payload);
			yield put({
				type: 'save',
				payload: response,
			});
		},
		*getTableData({ payload, callback }, { call, put }) {
			const response = yield call(tableurl, payload);
			yield put({
				type: 'setTableData',
				payload: response,
			});
			if (callback) callback();
		}
	},

	reducers: {
        //获取数据后更新state值
		setTableData(state, action) {
			console.log('data')
			return {
				...state,
				data: action.payload,
			};
		},
	},
};

4、service下新增接口tableurl:

5、mock数据:在mock文件夹下新建文件或者在api中新增对应接口,如下图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值