简介:Element Plus 中的 el-form 是一个表单组件,用于快速构建表单并进行数据校验。它提供了丰富的表单元素和验证规则,使表单开发变得更加简单和高效。可以搭配el-dialog实现当前页面的登录、注册页 ,这两天在vue3中用到了表单登录,特意记录一下,这里没有封装,直接使用的。

效果:

1、登录页面,弹出框表单

邮箱登录用户名登录我已阅读并同意服务条款、隐私政策请阅读并同意协议登录中......登录获取验证码{{ registerNum }}秒后重发我已阅读并同意服务条款、隐私政策请阅读并同意协议注册

2、相关参数,多去少补

import { ElMessage } from 'element-plus'//import { ref,reactive,toRefs,computed } from 'vue';//import { useStore } from 'vuex';import { useRouter, useRoute } from 'vue-router';//const store = useStore();const router = useRouter();//const route = useRoute();//const state = reactive({});import { reactive, ref, onMounted } from 'vue';import func from 'vue-temp/vue-editor-bridge';const loginStatus = ref(true);//用户/邮箱const isLoading = ref(false); //按钮loadingimport type { FormInstance, FormRules } from 'element-plus';const ruleFormRef = ref()const ruleFormRef2 = ref()// 用户登录还是邮箱登录function changeEmail() {loginStatus.value = !loginStatus.value;}// 登录const loginForm = reactive({code: "",email: "",password: "",smsCode: "",username: "",uuid: ""})const loginRules = reactive({username: [{required: true,message: '用户名为4~16字符之间(中文、字母、数字或下划线)',min: 6, max: 18,trigger: 'blur',},],email: [{ required: true, message: '请填写正确的邮箱格式', trigger: 'blur' },{ min: 6, max: 18, message: '邮箱字符为6~18之间', trigger: 'blur' },],password: [{required: true,message: '密码为6~18位字母、数字和符号',min: 6, max: 18,trigger: 'blur',},// { min: 6, max: 18, message: '密码字符为6~18之间', trigger: 'blur' },],})// 注册const registerForm = reactive({code: "",email: "",password: "",smsCode: "",username: "",uuid: ""})const registerRules = reactive({username: [{ required: true, message: '用户名为4~16位之间字符', trigger: 'blur', },{ min: 4, max: 16, message: '4~16位中文、字母、数字或下划线', trigger: 'blur' },],email: [{ required: true, message: '请填写正确的邮箱格式', trigger: 'blur' },{ min: 6, max: 18, message: '邮箱为6~18位之间字符', trigger: 'blur' },],password: [{required: true,message: '密码为6~18位字符,必须包含字母、数字和符号',min: 6, max: 18,trigger: 'blur',},],smsCode: [{required: true,message: '请输入验证码',trigger: 'blur',},],})const checked1 = ref(false);//登录协议const userDeal = ref(false);//提示用户const registerCks = ref(false);//注册协议const registerDeal = ref(false);//注册状态const authInfo = reactive({})//注册uuid//登录信息const loginInfo = reactive({})// const formLoading = ref(false);//弹框默认const dialogFormVisible = ref(false);//tab默认选中const activeName = ref('login_one')//注册/登录const loginOrRetister = ref(true);//验证码时间const registerNum = ref(180);//文字还是验证码const authCodeNum = ref(true);

3、事件方法

// tab切换事件function handleClick(tab, event) {const iAgree = document.querySelector(".form_footer");if (activeName.value === "register_two") {// 注册loginOrRetister.value = true;iAgree.style.display = "none";} else if (activeName.value === "login_one") {// 登录loginOrRetister.value = false;iAgree.style.display = "block";}}// 右上角注册/登录状态切换function choseResgister() {if (loginOrRetister.value === true) {loginOrRetister.value = false;activeName.value = "register_two";} else if (loginOrRetister.value === false) {loginOrRetister.value = true;activeName.value = "login_one";}}

这里的Element Plus组件使用的全局引入,所以可以直接使用;

样式大家自己写下吧!

然后点击登录、注册时,表单里的输入框验证,也有做,但是有时管用,有时不管用,这里给大家文档地址,自己看吧!

表单验证https://element-plus.org/zh-CN/component/form.html#%E8%A1%A8%E5%8D%95%E6%A0%A1%E9%AA%8C​​​​​​​

表单校验详情页https://element-plus.run/#eyJzcmMvQXBwLnZ1ZSI6Ijx0ZW1wbGF0ZT5cbiAgPGVsLWZvcm1cbiAgICByZWY9XCJydWxlRm9ybVJlZlwiXG4gICAgOm1vZGVsPVwicnVsZUZvcm1cIlxuICAgIDpydWxlcz1cInJ1bGVzXCJcbiAgICBsYWJlbC13aWR0aD1cIjEyMHB4XCJcbiAgICBjbGFzcz1cImRlbW8tcnVsZUZvcm1cIlxuICAgIDpzaXplPVwiZm9ybVNpemVcIlxuICAgIHN0YXR1cy1pY29uXG4gID5cbiAgICA8ZWwtZm9ybS1pdGVtIGxhYmVsPVwiQWN0aXZpdHkgb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