用vue3和element-plus完成一个注册界面,注册需要的信息有用户账号,昵称,登录密码,确认密码,出生年月,电话号码,还有一个字母的验证码。然后有一个注册按钮,点击按钮后,出现一个对话框显示注册成功,并有一个确定按钮
时间: 2024-06-11 10:09:41 浏览: 217
以下是使用Vue3和Element-Plus完成的注册界面代码:
```html
<template>
<div class="register">
<el-form ref="form" :model="form" :rules="rules" label-width="100px">
<el-form-item label="账号" prop="username">
<el-input v-model="form.username"></el-input>
</el-form-item>
<el-form-item label="昵称" prop="nickname">
<el-input v-model="form.nickname"></el-input>
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input type="password" v-model="form.password"></el-input>
</el-form-item>
<el-form-item label="确认密码" prop="confirmPassword">
<el-input type="password" v-model="form.confirmPassword"></el-input>
</el-form-item>
<el-form-item label="出生年月" prop="birthday">
<el-date-picker type="date" v-model="form.birthday"></el-date-picker>
</el-form-item>
<el-form-item label="电话号码" prop="phoneNumber">
<el-input v-model="form.phoneNumber"></el-input>
</el-form-item>
<el-form-item label="验证码" prop="captcha">
<el-input v-model="form.captcha"></el-input>
<el-button @click="getCaptcha" :disabled="countDown > 0">{{ countDown > 0 ? `${countDown}s后重新获取` : '获取验证码' }}</el-button>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="register">注册</el-button>
</el-form-item>
</el-form>
<el-dialog title="注册成功" :visible.sync="successDialogVisible">
<p>恭喜您,注册成功!</p>
<span slot="footer" class="dialog-footer">
<el-button type="primary" @click="successDialogVisible = false">确定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
import { ref } from 'vue'
import { ElForm, ElFormItem, ElInput, ElButton, ElDatePicker, ElDialog } from 'element-plus'
export default {
components: {
ElForm,
ElFormItem,
ElInput,
ElButton,
ElDatePicker,
ElDialog
},
setup() {
const form = ref({
username: '',
nickname: '',
password: '',
confirmPassword: '',
birthday: '',
phoneNumber: '',
captcha: ''
})
const rules = ref({
username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
nickname: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
confirmPassword: [{ required: true, message: '请再次输入密码', trigger: 'blur' }],
birthday: [{ required: true, message: '请选择出生年月', trigger: 'blur' }],
phoneNumber: [{ required: true, message: '请输入电话号码', trigger: 'blur' }],
captcha: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
})
const countDown = ref(0)
const successDialogVisible = ref(false)
const getCaptcha = () => {
// 发送获取验证码请求
countDown.value = 60
const timer = setInterval(() => {
if (countDown.value > 0) {
countDown.value -= 1
} else {
clearInterval(timer)
}
}, 1000)
}
const register = () => {
form.value.confirmPassword = form.value.password // 确认密码栏同步修改
form.value.birthday = form.value.birthday.toString() // 将日期对象转为字符串
// 发送注册请求
successDialogVisible.value = true
}
return {
form,
rules,
countDown,
successDialogVisible,
getCaptcha,
register
}
}
}
</script>
<style scoped>
.register {
max-width: 600px;
margin: 0 auto;
}
</style>
```
在代码中,我们使用了Element-Plus提供的表单组件,包括`ElForm`、`ElFormItem`、`ElInput`、`ElDatePicker`等,并提供了相应的`v-model`和`prop`绑定数据和校验规则。
同时,我们使用了Vue3的响应式特性,使用`ref`包装数据,并在`setup`函数中返回数据和方法,以供模板使用。
在点击注册按钮后,向后端发送注册请求,并在注册成功后弹出对话框显示结果。验证码的获取使用了一个倒计时计时器,防止用户频繁获取验证码。
阅读全文