## 改造修改页 1. 基于dva数据流改造需要做如下修改 * model修改为如下代码 ~~~ import { message } from 'antd'; import router from 'umi/router'; import { list, detail, submit } from '../services/demo'; export default { namespace: 'demo', state: { data: { list: [], pagination: {}, }, detail: {}, }, effects: { *fetchList({ payload }, { call, put }) { const response = yield call(list, payload); if (response.success) { yield put({ type: 'saveList', payload: { list: response.data.records, pagination: { total: response.data.total, current: response.data.current, pageSize: response.data.size, }, }, }); } }, *fetchDetail({ payload }, { call, put }) { const response = yield call(detail, payload); if (response.success) { yield put({ type: 'saveDetail', payload: { detail: response.data, }, }); } }, *submit({ payload }, { call }) { const response = yield call(submit, payload); if (response.success) { message.success('提交成功'); router.push('/platform/demo'); } }, }, reducers: { saveList(state, action) { return { ...state, data: action.payload, }; }, saveDetail(state, action) { return { ...state, detail: action.payload.detail, }; }, }, }; ~~~ * 增加connect ~~~ @connect(({ demo, loading }) => ({ demo, loading: loading.models.demo, })) @Form.create() class DemoEdit extends PureComponent { } ~~~ * 删掉state,修改componentWillMount,使用dispatch调用数据流进行数据加载 ~~~ componentWillMount() { const { dispatch, match: { params: { id }, }, } = this.props; dispatch({ type: 'demo/fetchDetail', payload: { id }, }); } ~~~ * 修改handelSubmit,使用dispatch调用数据流进行表单提交 ~~~ handleSubmit = e => { e.preventDefault(); const { form, dispatch, match: { params: { id }, }, } = this.props; form.validateFieldsAndScroll((err, values) => { if (!err) { const params = { id, ...values, date: func.format(values.date), }; dispatch({ type: 'demo/submit', payload: params, }); } }); }; ~~~ * 修改render下原先state中data的获取,改为从props获取并将data改为detail ~~~ const { form: { getFieldDecorator }, demo: { detail }, } = this.props; ~~~ 2. 打开系统,修改页数据加载成功 ![](https://box.kancloud.cn/687a595f3359dcdffff799e477d1d1ee_3350x1284.png) 3. 点击提交,接口调用与传参页正确 ![](https://box.kancloud.cn/d25a04aafbda53dc9f5286a8a04fca9c_2386x1180.png) 4. 附上完整代码 ~~~ import React, { PureComponent } from 'react'; import { Form, Input, Card, Button, DatePicker } from 'antd'; import { connect } from 'dva'; import moment from 'moment'; import Panel from '../../../components/Panel'; import styles from '../../../layouts/Sword.less'; import func from '../../../utils/Func'; const FormItem = Form.Item; const { TextArea } = Input; @connect(({ demo, loading }) => ({ demo, loading: loading.models.demo, })) @Form.create() class DemoEdit extends PureComponent { componentWillMount() { const { dispatch, match: { params: { id }, }, } = this.props; dispatch({ type: 'demo/fetchDetail', payload: { id }, }); } handleSubmit = e => { e.preventDefault(); const { form, dispatch, match: { params: { id }, }, } = this.props; form.validateFieldsAndScroll((err, values) => { if (!err) { const params = { id, ...values, date: func.format(values.date), }; dispatch({ type: 'demo/submit', payload: params, }); } }); }; render() { const { form: { getFieldDecorator }, demo: { detail }, } = this.props; const formItemLayout = { labelCol: { xs: { span: 24 }, sm: { span: 7 }, }, wrapperCol: { xs: { span: 24 }, sm: { span: 12 }, md: { span: 10 }, }, }; const action = ( <Button type="primary" onClick={this.handleSubmit}> 提交 </Button> ); return ( <Panel title="修改" back="/platform/demo" action={action}> <Form hideRequiredMark style={{ marginTop: 8 }}> <Card title="基本信息" className={styles.card} bordered={false}> <FormItem {...formItemLayout} label="标题"> {getFieldDecorator('title', { rules: [ { required: true, message: '请输入标题', }, ], initialValue: detail.title, })(<Input placeholder="请输入标题" />)} </FormItem> <FormItem {...formItemLayout} label="日期"> {getFieldDecorator('date', { rules: [ { required: true, message: '请输入日期', }, ], initialValue: moment(detail.date, 'YYYY-MM-DD HH:mm:ss'), })( <DatePicker style={{ width: '100%' }} format="YYYY-MM-DD HH:mm:ss" showTime={{ defaultValue: moment('00:00:00', 'HH:mm:ss') }} /> )} </FormItem> <FormItem {...formItemLayout} label="内容"> {getFieldDecorator('content', { initialValue: detail.content, })(<TextArea style={{ minHeight: 32 }} placeholder="请输入内容" rows={10} />)} </FormItem> </Card> </Form> </Panel> ); } } export default DemoEdit; ~~~