1、先创建修饰好页面
Login.vue:
<template>
<div class="login_container">
<div class="login_box">
<div class="avatar_box">
<img src="../assets/logo.png" alt="" />
</div>
<el-form class="login_form">
<el-form-item prop="username">
<el-input placeholder="用户名" prefix-icon="iconfont icon-yonghu">
</el-input>
</el-form-item>
<el-form-item prop="password">
<el-input
placeholder="密码"
prefix-icon="iconfont icon-mima"
type="password"
>
</el-input>
</el-form-item>
<el-form-item class="login_button">
<el-button type="primary">登陆</el-button>
<el-button type="info">重置</el-button>
</el-form-item>
</el-form>
</div>
</div>
</template>
<script>
export default {}
</script>
<style lang="less" scoped>
.login_container {
background-color: #2b4b6b;
height: 100%;
}
.login_box {
width: 450px;
height: 300px;
background-color: #fff;
border-radius: 3px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.avatar_box {
width: 100px;
height: 100px;
border: 1px solid #eee;
border-radius: 50%;
padding: 10px;
box-shadow: 0 0 10px #ddd;
position: absolute;
left: 50%;
transform: translate(-50%, -50%);
background-color: #fff;
img {
width: 100%;
height: 100%;
border-radius: 50%;
background-color: #eee;
}
}
.login_form {
position: absolute;
bottom: 0;
width: 100%;
padding: 0 20px;
box-sizing: border-box;
}
.login_button {
display: flex;
justify-content: flex-end;
}
</style>
2、利用v-model 指令,进行数据绑定,轻松实现表单输入和应用状态之间的双向绑定
Login.vue:
<template>
<div class="login_container">
<div class="login_box">
<div class="avatar_box">
<img src="../assets/logo.png" alt="" />
</div>
<el-form class="login_form" :model="loginForm">
<el-form-item>
<el-input
placeholder="用户名"
prefix-icon="iconfont icon-yonghu"
v-model="loginForm.username"
>
</el-input>
</el-form-item>
<el-form-item>
<el-input
placeholder="密码"
prefix-icon="iconfont icon-mima"
type="password"
v-model="loginForm.password"
>
</el-input>
</el-form-item>
<el-form-item class="login_button">
<el-button type="primary">登陆</el-button>
<el-button type="info">重置</el-button>
</el-form-item>
</el-form>
</div>
</div>
</template>
<script>
export default {
data() {
return {
loginForm: {
username: 'admin',
password: '123456',
},
}
},
}
</script>
3、Form 组件提供了表单验证的功能,只需要通过 rules 属性传入约定的验证规则,并将 Form-Item的 prop 属性设置为需校验的字段名即可
<template>
<div class="login_container">
<div class="login_box">
<div class="avatar_box">
<img src="../assets/logo.png" alt="" />
</div>
<el-form class="login_form" :model="loginForm" :rules="loginFormRules">
<el-form-item prop="username">
<el-input
placeholder="用户名"
prefix-icon="iconfont icon-yonghu"
v-model="loginForm.username"
>
</el-input>
</el-form-item>
<el-form-item prop="password">
<el-input
placeholder="密码"
prefix-icon="iconfont icon-mima"
type="password"
v-model="loginForm.password"
>
</el-input>
</el-form-item>
<el-form-item class="login_button">
<el-button type="primary">登陆</el-button>
<el-button type="info">重置</el-button>
</el-form-item>
</el-form>
</div>
</div>
</template>
<script>
export default {
data() {
return {
loginForm: {
username: 'admin',
password: '123456',
},
loginFormRules: {
username: [
{ required: true, message: '请输入用户名称', trigger: 'blur' },
{ min: 3, max: 10, message: '长度在3-10字符中' },
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' },
{ min: 6, max: 15, message: '长度在6-15字符中' },
],
},
}
},
}
</script>
3、利用resetFields方法重置表单数据
重置后:
<template>
<div class="login_container">
<div class="login_box">
<div class="avatar_box">
<img src="../assets/logo.png" alt="" />
</div>
<el-form
class="login_form"
:model="loginForm"
:rules="loginFormRules"
ref="loginFormRef"
>
<el-form-item prop="username">
<el-input
placeholder="用户名"
prefix-icon="iconfont icon-yonghu"
v-model="loginForm.username"
>
</el-input>
</el-form-item>
<el-form-item prop="password">
<el-input
placeholder="密码"
prefix-icon="iconfont icon-mima"
type="password"
v-model="loginForm.password"
>
</el-input>
</el-form-item>
<el-form-item class="login_button">
<el-button type="primary">登陆</el-button>
<el-button type="info" @click="resetLoginForm">重置</el-button>
</el-form-item>
</el-form>
</div>
</div>
</template>
<script>
export default {
data() {
return {
loginForm: {
username: '',
password: '',
},
loginFormRules: {
username: [
{ required: true, message: '请输入用户名称', trigger: 'blur' },
{ min: 3, max: 10, message: '长度在3-10字符中' },
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' },
{ min: 6, max: 15, message: '长度在6-15字符中' },
],
},
}
},
methods: {
resetLoginForm() {
console.log(this)
this.$refs.loginFormRef.resetFields()
},
},
}
</script>
4、登陆之前进行预校验(validate),校验成功之后向API发出Post请求,获得res结果,判断res.status是否等于200,成功之后把Token保存到客户端sessionStorage中
Login.vue:
<template>
<div class="login_container">
<div class="login_box">
<div class="avatar_box">
<img src="../assets/logo.png" alt="" />
</div>
<el-form
class="login_form"
:model="loginForm"
:rules="loginFormRules"
ref="loginFormRef"
>
<el-form-item prop="username">
<el-input
placeholder="用户名"
prefix-icon="iconfont icon-yonghu"
v-model="loginForm.username"
>
</el-input>
</el-form-item>
<el-form-item prop="password">
<el-input
placeholder="密码"
prefix-icon="iconfont icon-mima"
type="password"
v-model="loginForm.password"
>
</el-input>
</el-form-item>
<el-form-item class="login_button">
<el-button type="primary" @click="login">登陆</el-button>
<el-button type="info" @click="resetLoginForm">重置</el-button>
</el-form-item>
</el-form>
</div>
</div>
</template>
<script>
export default {
data() {
return {
loginForm: {
username: '',
password: '',
},
loginFormRules: {
username: [
{ required: true, message: '请输入用户名称', trigger: 'blur' },
{ min: 3, max: 10, message: '长度在3-10字符中' },
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' },
{ min: 6, max: 15, message: '长度在6-15字符中' },
],
},
}
},
methods: {
resetLoginForm() {
console.log(this)
this.$refs.loginFormRef.resetFields()
},
login() {
this.$refs.loginFormRef.validate(async (valid) => {
if (!valid) return
const { data: res } = await this.$http.post('login', this.loginForm)
if (res.meta.status !== 200) return this.$message.error('登陆失败!')
this.$message.success('登陆成功!')
window.sessionStorage.setItem('token', res.data.token)
this.$router.push('/home')
})
},
},
}
</script>
5、挂载路由导航守卫,没有登陆前不能进主页面
router/index.js:
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/shop_login.vue'
import Home from '../components/shop_home.vue'
import Home1 from '../components/ew_re.vue'
Vue.use(VueRouter)
const routes = [
{ path: '/', redirect: '/login' },
{ path: '/login', component: Login },
{ path: '/home', component: Home },
{ path: '/home1', component: Home1 },
]
const router = new VueRouter({
routes,
})
router.beforeEach((to, from, next) => {
if (to.path === '/login') return next()
const tokenStr = window.sessionStorage.getItem('token')
if (!tokenStr) return next('/login')
next()
})
export default router
6、退出登陆功能,把Token值去掉即可
Home.vue:
<template>
<el-button type="info" @click="logout">退出</el-button>
</template>
<script>
export default {
methods: {
logout() {
window.sessionStorage.clear()
this.$router.push('/login')
},
},
}
</script>
<style lang="less" scoped></style>
|