开发完一个项目,如今将总结一下项目中遇到的一些问题。也探讨一下问题出现的缘由。话很少说,踩坑总结又开始了。后端
<Form.Item label="用户名"> {getFieldDecorator('username', { rules: [ { required: true, message: '用户名不能为空' }, { validator: handleUserName} // 使用validator方法 后面写本身校验的方法 ], })(<Input placeholder="请输入用户名" style={{width:260}}/>)} </Form.Item>
而后编写本身定义的validator方法,注意不管何种状况都要调用callback(),不然你页面其余校验的Form.item的提示将不显示。antd
ps:我这里的业务逻辑是用户输入名字后,向后端检验,根据返回结果显示不一样的提示。app
const handleUserName = function ( rule, value, callback ) { if(value){ dispatch({ ... }).then(result => { // 使用.then()能够接受 model方法的return的数据 if(result.code === 400){ callback(result.msg); // 注意!!不管何种状况都要调用callback(),不然其余地方的验证会不显示! } else { callback(); // 注意!!不管何种状况都要调用callback(),不然其余地方的验证会不显示! } }); } else { callback(); // 注意!!不管何种状况都要调用callback(),不然其余地方的验证会不显示! } }) };
如下例子为网址中最后一个字段
const pathArr = location.pathname.split('/'); const objectId = pathArr[pathArr.length-1]
import pathToRegexp from 'path-to-regexp'; const a = pathToRegexp('/employmentManagement/employmentDetail/:employmentId').exec(location.pathname); console.log(a[1])
界面大概长这样(原型图):dom

实现难点:
一、点击div后变成input输入框
二、点击新增后,在下方多出一个input输入框,而且带有新增按钮ui
evaluatorList.map((item, index)=>( // 把不是当前选择的current显示为正常的div // 点击时触发showEdit(),把current当前点击的index、把inputVisible置为true {(current!== index) &&( <div onClick={showEdit.bind(this,item,index)} className={styles.name}> {item.evaluatorName} </div> )} // 当inputVisible为true而且 current是当前index时,div不显示,显示EditSelect 组件(该组件是一个信息输入组件) { (inputVisible && current === index) &&( <EditSelect id='selectFocusId'/> )} <div className={styles.operation}></div> ))
正常的思路应该是经过操控数据来实现新增的样式变化。
由于上面的样式都是经过遍历evaluatorList来展开的。点击新增的时候,咱们在evaluatorList的对应位置,插入一个空对象。这时页面已经快达到理想效果了。
再把inputVisible置为true、 current: index+1,这样能够把当前新增的div变成输入框状态。this
// 新增 const handleAdd =(item, index)=>{ if(!isAdd){ // isAdd默认为false,这里判断是防止点击新增时这个没有关闭,再次打开一个新的新增 let tempList = JSON.parse(JSON.stringify(evaluatorList)) tempList.splice(index+1,0,{}) dispatch({ type:"approvalManagement/updateStates", payload:{ evaluatorList: tempList, inputVisible: true, current: index+1, isAdd: true } }) } }
写完了,overlua