登录页面Login.vue
删除src
目录下的Login.vue,新建login
文件夹,在login
文件夹下新建index.vue
,内容如下:
<template>
<div class="login-container">
<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
<div class="title-container">
<h3 class="title">系统登录</h3>
</div>
<el-form-item prop="username">
<!-- <span class="svg-container">
<svg-icon icon-class="user" />
</span> -->
<el-input ref="username" v-model="loginForm.username" placeholder="请输入管理员账号" name="username" type="text" tabindex="1" auto-complete="on" />
</el-form-item>
<el-form-item prop="password">
<!-- <span class="svg-container">
<svg-icon icon-class="password" />
</span> -->
<el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="请输入管理员密码" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
<!-- <span class="show-pwd" @click="showPwd">
<svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
</span> -->
</el-form-item>
<el-button size="medium" :loading="loading" type="success" style="width: 100%; margin-bottom: 30px" @click.native.prevent="handleLogin">立即登录</el-button>
</el-form>
</div>
</template>
<script>
export default {
name: 'LoginPage',
data() {
const validateUsername = (rule, value, callback) => {
if (!value) {
callback(new Error('请输入管理员账号'));
} else {
callback();
}
};
const validatePassword = (rule, value, callback) => {
if (value.length < 6) {
callback(new Error('密码不能少于6位'));
} else {
callback();
}
};
return {
loginForm: {
username: 'admin',
password: '123456'
},
loginRules: {
username: [
{ required: true, trigger: 'blur', validator: validateUsername }
],
password: [
{ required: true, trigger: 'blur', validator: validatePassword }
]
},
loading: false,
passwordType: 'password',
redirect: undefined
};
},
watch: {
$route: {
handler: function (route) {
this.redirect = route.query && route.query.redirect;
},
immediate: true
}
},
methods: {
showPwd() {
if (this.passwordType === 'password') {
this.passwordType = '';
} else {
this.passwordType = 'password';
}
this.$nextTick(() => {
this.$refs.password.focus();
});
},
handleLogin() {
this.$refs.loginForm.validate((valid) => {
if (valid) {
this.loading = true;
this.$store
.dispatch('account/login', this.loginForm)
.then(() => {
this.$router.push({ path: this.redirect || '/' });
this.loading = false;
})
.catch((err) => {
this.loading = false;
this.$message.error(err.msg);
});
} else {
return false;
}
});
}
}
};
</script>
<style lang="scss" scoped>
$bg: #283443;
$light_gray: #eee;
$dark_gray: #889aa4;
$cursor: #fff;
$black: #000;
$white: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
.login-container .el-input input {
color: $black;
}
}
/* reset element-ui css */
.login-container {
.el-input {
display: inline-block;
height: 47px;
width: 85%;
input {
background: transparent;
border: 0px;
-webkit-appearance: none;
border-radius: 0px;
padding: 12px 5px 12px 15px;
color: $black;
height: 47px;
caret-color: $black;
&:-webkit-autofill {
box-shadow: 0 0 0px 1000px $bg inset !important;
-webkit-text-fill-color: $cursor !important;
}
&::-webkit-input-placeholder {
font-size: 13px;
color: #889aa4;
}
}
}
.el-form-item {
border: 1px solid rgba(255, 255, 255, 0.1);
background: rgba(0, 0, 0, 0.1);
border-radius: 5px;
color: #454545;
}
}
.login-container {
min-height: 100%;
width: 100%;
overflow: hidden;
background-image: url('../../assets/bg.jpg');
background-size: cover;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
.login-form {
width: 520px;
height: 420px;
max-width: 100%;
padding: 60px;
overflow: hidden;
background: $light_gray;
}
.svg-container {
padding: 6px 5px 6px 15px;
color: $black;
vertical-align: middle;
width: 30px;
display: inline-block;
}
.title-container {
position: relative;
.title {
font-size: 26px;
color: $black;
margin: 0px auto 40px auto;
text-align: center;
font-weight: bold;
}
}
.show-pwd {
position: absolute;
right: 10px;
top: 7px;
font-size: 16px;
color: $black;
cursor: pointer;
user-select: none;
}
}
</style>
其中svg-icon
是全局组件,现在没有,先注释,后续加上。
其中this.$store
是本地存储,现在没有,先不管。
然后在src/router/index.js
文件中引入登录页import Login from '@/views/login/index'
然后浏览器中登录页面展示如下:
引入全局样式文件
当然了,到这里,你的也许会有些不同。你需要现在src/App.vue
中删除img
标签
然后还需要引入全局样式文件,在src
目录下,新建目录styles
,然后新建文件index.scss
,内容如下:
body {
height: 100%;
margin: 0;
padding: 0;
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
text-rendering: optimizeLegibility;
font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif;
}
label {
font-weight: 700;
}
html {
height: 100%;
box-sizing: border-box;
}
#app {
height: 100%;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
a:focus,
a:active {
outline: none;
}
a,
a:focus,
a:hover {
cursor: pointer;
color: inherit;
text-decoration: none;
}
div:focus {
outline: none;
}
.clearfix {
&:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
}
然后在main.js
中引入import '@/styles/index.scss'