效果如图
现在问题是,每次点击提交之后,都会如下报错:
查了一遍之后,有一种说法是el-date-picker本身支持字符串和date object两种类型,但是json中的字符串绑定之后在效验时就不是date object,所以出现了异常。所以加上that.newForm.useTime=moment(that.newForm.useTime).toDate();进行转换可是还是报错。
代码图片和源码如下,请大神指教,谢谢!
HTML:
rules:
js:
(忽略params里freeTime:that.newForm.useTime,少了一个newForm,修改的时候粗心了)
源码:
html:
<el-button type="primary" icon="plus" @click="newDialogVisible = true">新增账号</el-button>
<el-dialog title="新增账号" v-model="newDialogVisible" @close="resetForm1('newForm')">
<el-form :model="newForm" :rules="rules1" ref="newForm" :label-width="formLabelWidth">
<el-form-item label="厂家名称" prop="nickName">
<el-input v-model="newForm.nickName" auto-complete="off" style="width:70%;"></el-input>
</el-form-item>
<el-form-item label="账号" prop="userAccount">
<el-input v-model="newForm.userAccount" @blur="checkName" auto-complete="off" style="width:70%;"></el-input>
</el-form-item>
<el-form-item label="密码" prop="userPasswd">
<el-input v-model="newForm.userPasswd" auto-complete="off" style="width:70%;"></el-input>
</el-form-item>
<el-form-item label="实体店最大数量"prop="maxNumber">
<el-input v-model.number="newForm.maxNumber" auto-complete="off" style="width:70%;"></el-input>
</el-form-item>
<el-form-item label="到期时间" prop="useTime">
<el-date-picker
v-model="newForm.useTime"
style="width:70%;"
type="date"
placeholder="选择日期"
>
</el-date-picker>
</el-form-item>
<el-form-item label="备注">
<el-input v-model="newForm.remark" auto-complete="off" style="width:70%;"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="primary" @click="addAcount('newForm')">保 存</el-button>
<el-button @click="resetForm1('newForm')">取 消</el-button>
</div>
</el-dialog>
js:
//添加
addAcount:function(formName){
let that = this
that.newForm.useTime=moment(that.newForm.useTime).format('YYYY-MM-DD');
// alert(that.newForm.useTime)
that.$refs[formName].validate((valid) => {
if (valid) {
// that.newForm.useTime=moment(that.newForm.useTime).toDate();
that.$http.post(config.URL+'platform/user/add',
{},
{
params:{
username:that.newForm.userAccount,
password:that.newForm.userPasswd,
nickname:that.newForm.nickName,
freeTime:that.newForm.useTime,
maxNumber:that.newForm.maxNumber,
remark:that.newForm.remark
},
emulateJSON:true
})
.then(
function(res){
if (res.body.code==1) {
that.show();
that.$refs[formName].resetFields();
that.newDialogVisible = false
}
else{
Message.error(res.body.statusMsg);
}
},
function(err){
Message.error('与服务器连接错误!');
})
} else {
console.log('error submit!!');
return false;
}
});
},
我是在从接口获取时间后直接放在input中报错,然后把时间放到new Date()中处理一下就不会报错了。