1.实现效果
2. 代码实现
2.1使用elementUI文档中Tabs标签页
2.2在components中新建两个文件 login.vue register.vue
login.vue
回车登录(
按钮添加native-type=”submit” 登录表单添加@submit.native.prevent
)
<template>
<div class="login">
<div class="login_form">
<p>后台管理系统</p>
<el-tabs v-model="activeName" @tab-click="handleClick" >
<el-tab-pane label="登录" name="first">
<el-form
:model="loginForm"
:rules="rules"
ref="loginForm"
@submit.native.prevent
>
<el-form-item label="" prop="account" class="elItem">
<el-input
type="text"
autocomplete="off"
v-model="loginForm.account"
prefix-icon="el-icon-user-solid"
placeholder="请输入用户名"
></el-input>
</el-form-item>
<el-form-item label="" prop="password">
<el-input
type="password"
autocomplete="off"
v-model="loginForm.password"
prefix-icon="el-icon-lock"
placeholder="请输入密码"
></el-input>
</el-form-item>
<el-form-item class="btns">
<el-button type="primary" @click="goToLogin" native-type="submit">登录</el-button>
<el-button @click="resetLoginForm">重置</el-button>
</el-form-item>
</el-form>
</el-tab-pane>
<el-tab-pane label="注册" name="second">
//注册组件
<register></register>
</el-tab-pane>
</el-tabs>
</div>
</div>
</template>
<script>
//引入注册组件
import register from '@/components/register';
export default {
data() {
var validateAccount = (rule, value, callback) => {
if (value === "") {
return callback(new Error("账号不能为空"));
} else if (value === "admin") {
callback();
} else {
callback(new Error("请输入正确的用户名"));
}
};
var validatePassword = (rule, value, callback) => {
if (value === "") {
callback(new Error("请输入密码"));
} else if (value === "123456") {
callback();
} else {
callback(new Error("请输入正确的密码"));
}
};
return {
loginForm: {
account: "",
password: "",
},
activeName:'first',//默认显示登录页面
rules: {
account: [
{
validator: validateAccount,
trigger: "blur",
},
],
password: [
{
validator: validatePassword,
trigger: "blur",
},
],
},
};
},
methods: {
//固定的账户密码判断实现简单的登录跳转功能,只能测试用
goToLogin() {
this.$refs["loginForm"].validate((valid) => {
if (valid) {
if (
this.loginForm.account != "admin" ||
this.loginForm.password != "123456"
) {
this.$message.error("账号密码不正确");
return false;
} else {
this.$message({ message: "登陆成功", type: "success" });
this.$router.push("/home");
}
} else {
this.$message.error("登陆失败");
return false;
}
});
},
resetLoginForm() {
this.$refs["loginForm"].resetFields();
},
handleClick(){}
},
components:{
register
}
};
</script>
<style scoped lang='less'>
.login {
width: 100%;
height: 100vh;
background-image: url("../assets/login/login.jpg");//背景图
background-size: 100% 100%;
background-position: center center;
overflow: auto;
position: relative;
.login_form {
width: 400px;
height: 360px;
position: absolute;
left: 78%;
top: 50%;
margin-left: -200px;
margin-top: -150px;
padding: 10px;
background: #fff;
border-radius: 10px;
box-shadow: 0 0 10px #ddd;
.btns {
display: flex;
justify-content: flex-end;
}
}
p {
font-size: 24px;
text-align: center;
font-weight: 600;
}
}
</style>
register.vue
<template>
<el-form
:model="ruleForm"
:rules="rules"
ref="ruleForm"
class="demo-ruleForm"
>
<el-form-item label="" prop="name"
><el-input
type="text"
autocomplete="off"
v-model="ruleForm.name"
prefix-icon="el-icon-user-solid"
placeholder="请输入用户名"
></el-input
></el-form-item>
<el-form-item label="" prop="pass"
><el-input
type="password"
autocomplete="off"
v-model="ruleForm.pass"
prefix-icon="el-icon-lock"
placeholder="请输入密码"
></el-input
></el-form-item>
<el-form-item label="" prop="checkPass"
><el-input
type="password"
autocomplete="off"
v-model="ruleForm.checkPass"
prefix-icon="el-icon-lock"
placeholder="请输入密码"
></el-input
></el-form-item>
<el-form-item class="btns">
<el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
<el-button @click="resetForm('ruleForm')">重置</el-button>
</el-form-item>
</el-form>
</template>
<script>
export default {
data() {
var validatePass = (rule, value, callback) => {
if (value === "") {
callback(new Error("请输入密码"));
} else {
if (this.ruleForm.checkPass !== "") {
this.$refs.ruleForm.validateField("checkPass");
}
callback();
}
};
var validatePass2 = (rule, value, callback) => {
if (value === "") {
callback(new Error("请再次输入密码"));
} else if (value !== this.ruleForm.pass) {
callback(new Error("两次输入密码不一致!"));
} else {
callback();
}
};
return {
activeName: "second",
ruleForm: {
name: "",
pass: "",
checkPass: "",
},
rules: {
name: [
{ required: true, message: "请输入您的名称", trigger: "blur" },
{ min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
],
pass: [{ required: true, validator: validatePass, trigger: "blur" }],
checkPass: [
{ required: true, validator: validatePass2, trigger: "blur" },
],
},
};
},
methods: {
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
this.$message({
type: "success",
message: "注册成功",
});
// this.activeName: 'first',
} else {
console.log("error submit!!");
return false;
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
},
},
};
</script>
<style scoped lang="less">
.btns {
display: flex;
justify-content: flex-end;
}
</style>
版权声明:本文为m0_59570698原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。