您的位置:首页 > 新闻 > 会展 > vue2的form利用插槽修改错误提示UI

vue2的form利用插槽修改错误提示UI

2025/9/18 6:20:33 来源:https://blog.csdn.net/wzp20092009/article/details/139494404  浏览:    关键词:vue2的form利用插槽修改错误提示UI

1. 需求

很多时候我们使用el-form想修改下错误提示的UI,比如table中使用form校验这类场景下错误提示的UI调整就非常重要。

2. 了解文档

Form-Item Scoped Slot

name说明
error自定义表单校验信息的显示方式,参数为 { error }

3.实际使用

html里使用错误的插槽将自定义错误插入。

<template><el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" ><el-form-item label="活动名称" prop="name"><el-input v-model="ruleForm.name"></el-input><template slot="error" slot-scope="itemScope"><!-- 自定义错误信息 --><div v-if="itemScope.error"><el-tooltip :content="itemScope.error" placement="top"><i class="el-icon-warning-outline sad-item-error" /></el-tooltip></div></template></el-form-item><el-form-item><el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button><el-button @click="resetForm('ruleForm')">重置</el-button></el-form-item><el-form>
</template>

javascript的代码大同小异

export default {data() {return {ruleForm: {name: ''},rules: {name: [{ required: true, message: '请输入活动名称', trigger: 'blur' },{ min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }]}};},methods: {submitForm(formName) {this.$refs[formName].validate((valid) => {if (valid) {alert('submit!');} else {console.log('error submit!!');return false;}});},resetForm(formName) {this.$refs[formName].resetFields();}}}

样式要做一些调整

.el-form-item{margin-bottom: 0;
}
/* 自己调整 */
.sad-item-error{position: absolute;color: #F56C6C;top: 0;left: 100%;line-height: 32px;
}

4. 结果

结果类似如下:
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com