【Vue】ElementUI实现登录注册
在Vue中使用ElementUI实现登录和注册功能的基本步骤如下:
- 安装ElementUI:
npm install element-ui --save
- 在Vue项目中引入ElementUI:
// main.js
import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
- 创建登录和注册组件:
<template>
<div>
<!-- 登录表单 -->
<el-form ref="loginForm" :model="loginForm" label-width="80px">
<el-form-item label="用户名">
<el-input v-model="loginForm.username" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="密码">
<el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="login">登录</el-button>
</el-form-item>
</el-form>
<!-- 注册表单 -->
<el-form ref="registerForm" :model="registerForm" label-width="80px">
<el-form-item label="用户名">
<el-input v-model="registerForm.username" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="密码">
<el-input type="password" v-model="registerForm.password" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="确认密码">
<el-input type="password" v-model="registerForm.confirmPassword" autocomplete="off"></el-input>
</el-form-item>
<el-form-item>
<el-button type="success" @click="register">注册</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
data() {
return {
loginForm: {
username: '',
password: ''
},
registerForm: {
username: '',
password: '',
confirmPassword: ''
}
}
},
methods: {
login() {
// 登录逻辑
},
register() {
// 注册逻辑
}
}
}
</script>
- 实现登录和注册逻辑:
export default {
// ...
methods: {
login() {
// 发送登录请求,例如:
this.$http.post('/api/login', this.loginForm)
.then(response => {
// 登录成功处理逻辑
})
.catch(error => {
// 登录失败处理逻辑
});
},
register() {
// 确认密码校验
if (this.registerForm.password !== this.registerForm.confirmPassword) {
this.$message.error('密码与确认密码不一致');
return;
}
// 发送注册请求,例如:
this.$http.post('/api/re
评论已关闭