一、首页设置大体的样式布局
1.首先建立一个login文件夹,在里面建立对应的login.vue文件
2.设置登录页面的背景图,在App.vue文件中使用router-view进行展示登录组件
3.先给App.vue的div元素设置高度100%,之后在login.vue里面去设置背景图
App.vue
<template>
<div class="app">
<router-view></router-view>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue"
export default defineComponent({
name: "App"
})
</script>
<style lang="less">
/* 先将主面板设置宽度,之后再去设置对应的组件login */
.app {
height: 100%;
}
</style>
login.vue
这里注意怎么使login组件垂直水平居中的样式
<template>
<div class="login">login</div>
</template>
<script lang="ts">
import { defineComponent } from "vue"
export default defineComponent({
name: "login"
})
</script>
<style lang="less" scoped>
.login {
/* 实现水平垂直居中 */
/* **display:flex**布局 justify-content定义水平方向的元素位置,align-items定义垂直方向的元素位置 */
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
background: url(../../assets/img/login-bg.svg);
}
</style>
二、开始使用组件化引用
1.首先建立一个cpns的文件夹,里面有一个login-panel.vue文件
2.之后在login.vue文件中引用这个组件
3.将登录页面的组件代码封装在login-panel.vue
login.vue
<template>
<div class="login">
//引用组件
<login-panel></login-panel>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue"
import LoginPanel from "./cpns/login-panel.vue"
export default defineComponent({
//注册组件
components: {
LoginPanel
}
})
</script>
<style lang="less" scoped>
.login {
/* 实现水平垂直居中 */
/* **display:flex**布局 justify-content定义水平方向的元素位置,align-items定义垂直方向的元素位置 */
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
background: url(../../assets/img/login-bg.svg);
}
</style>
login-panel.vue
<template>
<div class="login-panel">
<h1>后台管理系统</h1>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue"
export default defineComponent({
name: "login-panel"
})
</script>
<style lang="less" scoped></style>
三.开始编写中间登录界面的主要代码
1.首先去element-plus官网找适合的模板样式,之后直接copy到login-panel.vue组件中,分别给对应的登添加相应的icon图标
(1)首先先安装npm install @element-plus/icons
(2)之后复制相应的图标代码avatar
(3)新的icon需要有一层el-icon去包裹才能生效
<el-icon style="vertical-align: middle"
><avatar class="tubiao"
/></el-icon>
(4)在对应的文件里面引用相应的文件,还得同时注册一下组件
import { Avatar, Cellphone } from "@element-plus/icons"
components: {
Avatar,
Cellphone,
}
2.建立账号登录和手机登录的两个组件文件login-account.vue和login-phone.vue
3.将封装好的组件引用到login-panel.vue对应的地方
<template>
<div class="login-panel">
<h1 class="title">后台管理系统</h1>
<!-- stretch这个是设置是否自适应宽度 -->
<el-tabs type="border-card" class="demo-tabs" stretch>
<el-tab-pane>
<template #label>
<span class="custom-tabs-label">
<el-icon><calendar /></el-icon>
<span>
<el-icon style="vertical-align: middle"
><avatar class="tubiao"
/></el-icon>
账号登录</span
>
</span>
</template>
<login-account></login-account>
</el-tab-pane>
<el-tab-pane>
<template #label>
<span class="custom-tabs-label">
<el-icon><calendar /></el-icon>
<span
><el-icon style="vertical-align: middle"
><cellphone class="tubiao" /></el-icon
>手机登录</span
>
</span>
</template>
<login-phone></login-phone>
</el-tab-pane>
</el-tabs>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue"
import { Avatar, Cellphone } from "@element-plus/icons"
import LoginPhone from "./login-phone.vue"
import LoginAccount from "./login-account.vue"
export default defineComponent({
name: "login-panel",
components: {
Avatar,
Cellphone,
LoginPhone,
LoginAccount
}
})
</script>
<style lang="less" scoped>
.login-panel {
margin-bottom: 100px;
width: 320px;
.title {
/* 使标题居中 */
text-align: center;
}
.account-control {
margin-top: 10px;
display: flex;
justify-content: space-between;
}
}
</style>
四、分别在对应的文件里面写对应的文本框login-account.vue和login-phone.vue
(1)login-account.vue
- 首先先选择合适的文本框
- 然后编写相应的响应式数据
- 编写相应的规则校验
<template>
<div class="login-account">
<el-form label-width="60px" :rules="rules" :model="account" ref="formRef">
<el-form-item label="账号" prop="name">
<el-input v-model="account.name" />
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input v-model="account.password" show-password />
</el-form-item>
</el-form>
</div>
</template>
<script lang="ts">
import { defineComponent, reactive } from "vue"
export default defineComponent({
name: "login-account",
components: {},
setup() {
const account = reactive({
name: "",
password: ""
})
// 编写规则
const rules = {
name: [
{
// 是否是必传
required: true,
message: "用户名是必传内容",
// 失去焦点是判断是否符合要求规则
trigger: "blur"
},
{
pattern: /^[a-z0-9]{5,10}$/,
message: "用户名必须是5-10个字母或者数字",
trigger: "blur"
}
],
password: [
{
required: true,
message: "密码是必传内容",
trigger: "blur"
},
{
pattern: /^[a-z0-9]{3,}$/,
message: "密码必须是3位以上的字母或者数字",
trigger: "blur"
}
]
}
return {
account,
rules
}
}
})
</script>
<style lang="less" scoped></style>
为了使代码具有简洁性,可以选择把规则校验抽离到一个单独的文件account-config.ts中,之后可以选择把那些没有响应的数据单独抽离到一个配置文件,然后再引用进来
account-config.ts
// 编写规则
export const rules = {
name: [
{
required: true,
message: '用户名是必传内容',
// 失去焦点是判断是否符合要求规则
trigger: 'blur'
},
{
pattern: /^[a-z0-9]{5,10}$/,
message: '用户名必须是5-10个字母或者数字',
trigger: 'blur'
}
],
password: [
{
required: true,
message: '密码是必传内容',
trigger: 'blur'
},
{
pattern: /^[a-z0-9]{3,}$/,
message: '密码必须是3位以上的字母或者数字',
trigger: 'blur'
}
]
}
login-account.vue
<template>
<div class="login-account">
<el-form label-width="60px" :rules="rules" :model="account" ref="formRef">
<el-form-item label="账号" prop="name">
<el-input v-model="account.name" />
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input v-model="account.password" show-password />
</el-form-item>
</el-form>
</div>
</template>
<script lang="ts">
import { defineComponent, reactive } from "vue"
import { rules } from "../config/account-config"
export default defineComponent({
name: "login-account",
components: {},
setup() {
const account = reactive({
name: "",
password: ""
})
return {
account,
rules
}
}
})
</script>
<style lang="less" scoped></style>
(2)login-phone.vue
具体和账号登录一样的步骤
- 首先先选择合适的文本框
- 然后编写相应的响应式数据
- 编写相应的规则校验
verify-code-config.ts
// 编写规则
export const rules = {
phone: [
{
required: true,
message: '手机是必传内容',
// 失去焦点是判断是否符合要求规则
trigger: 'blur'
},
{
pattern: /^[a-z0-9]{5,10}$/,
message: '手机号必须是5-10个字母或者数字',
trigger: 'blur'
}
],
verifycode: [
{
required: true,
message: '验证码是必传内容',
trigger: 'blur'
},
{
pattern: /^[a-z0-9]{3,}$/,
message: '验证必须是3位以上的字母或者数字',
trigger: 'blur'
}
]
}
login-phone.vue
<template>
<div>
<!-- :model="account"将下面的input的最新的值给传给form,如何进行验证-->
<el-form label-width="70px" :rules="rules" :model="shouji">
<el-form-item label="手机号" prop="phone">
<el-input v-model="shouji.phone"></el-input>
</el-form-item>
<el-form-item label="验证码" prop="verifycode">
<div class="get-code">
<el-input v-model="shouji.verifycode"></el-input>
<el-button type="primary" class="get-bin">获取验证码</el-button>
</div>
</el-form-item>
</el-form>
</div>
</template>
<script lang="ts">
import { defineComponent, reactive } from "vue"
import { rules } from "../config/verify-code-config.ts"
export default defineComponent({
setup() {
const shouji = reactive({
phone: "",
verifycode: ""
})
return {
shouji,
rules
}
}
})
</script>
<style scoped>
.get-code {
display: flex;
}
.get-bin {
margin-left: 8px;
}
</style>
五、对登录进行验证以及校验
1.先给点击登录按钮绑定点击事件
2.然后在对应的组件里面也绑定对应的事件
3.利用ts语法拿到账号登录组件
4.在账号登录组件里面拿到对应的表单验证的布尔值,当点击登录触发事件时,判断布尔值,决定是否能够登录成功
login-panel.vue
// 拿到对应的组件LoginAccount
const accountRef = ref<InstanceType<typeof LoginAccount>>()
// 2.定义方法
const handleLoginClick = () => {
if (currentTab.value === "account") {
// 为什么加?,是因为确保一开始是没有值的,这里就是表示可由可无
accountRef.value?.loginAction(isKeepPassword.value)
} else {
console.log("phoneRef调用loginAction")
}
}
login-ccount.vue
<el-form label-width="60px" :rules="rules" :model="account" ref="formRef">
<el-form-item label="账号" prop="name">
<el-input v-model="account.name" />
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input v-model="account.password" show-password />
</el-form-item>
</el-form>
// formRe拿到上面的表单,InstanceType泛型
const formRef = ref<InstanceType<typeof ElForm>>()
const loginAction = (isKeepPassword: boolean) => {
// 拿到formRe.value,即表单的对应数据,validate,拿到表单对应的验证值(布尔值)
formRef.value?.validate((valid) => {
console.log(valid)
})
}
六、LocalCache账号密码的本地存储
1.首先建立一个util文件夹,然后里面写LocalCache的getCache这些方法
2.然后在login-account.vue里面根据点击登录之后是否记住密码的布尔值去 localCache.setCache("name", account.name)
login-account.vue
const loginAction = (isKeepPassword: boolean) => {
formRef.value?.validate((valid) => {
if (valid) {
// 1.判断是否需要记住密码
if (isKeepPassword) {
// 本地缓存
localCache.setCache("name", account.name)
localCache.setCache("password", account.password)
} else {
localCache.deleteCache("name")
localCache.deleteCache("password")
}
// 2.开始进行登录验证
store.dispatch("login/accountLoginAction", { ...account })
}
})
}
cache.ts
class LocalCache {
setCache(key: string, value: any) {
window.localStorage.setItem(key, JSON.stringify(value))
}
getCache(key: string) {
// obj->string->obj
const value = window.localStorage.getItem(key)
if (value) {
return JSON.parse(value)
}
}
deleteCache(key: string) {
window.localStorage.removeItem(key)
}
clearCache() {
window.localStorage.clear()
}
}
export default new LocalCache()
七、账号登录的Vuex使用
1.首先先创建一个store文件夹,里面分别有index.ts和type.ts,还有login对应的login.ts和type.ts
2.安装vuex:npm install vuex --save
3.开始写vuex模块,首先先写根store,里面注册对应的login模块
import { createStore, Store, useStore as useVuexStore } from 'vuex'
import login from './login/login'
import { IRootState, IStoreType } from './types'
//IRootState根store类型
const store = createStore<IRootState>({
state() {
return {
name: 'ZJE',
age: 18,
}
},
mutations: {
},
getters: {},
actions: {
},
modules: {
login,
}
})
export default store
4.开始在login.ts里面写点击登录时触发的事件
login.ts
import { Module } from 'vuex'
import localCache from '@/utils/cache'
import router from '@/router'
import { ILoginState } from './types'
import { IRootState } from '../types'
// ILoginState模块的store类型,IRootState根store类型
const loginModule: Module<ILoginState, IRootState> = {
namespaced: true,
state() {
return {
token: '',
userInfo: {}
}
},
getters: {},
mutations: {
},
actions: {
// store.dispatch("login/accountLoginAction", { ...account })会调用这里
async accountLoginAction({ commit }, payload: any) {
console.log("执行accountLoginAction", payload)
}
}
}
export default loginModule
type.ts
export interface ILoginState {
token: string
userInfo: any
}
login-account.vue
5.在state里面开始发送请求接口函数,然后把登录成功获取的data存储在本地localCache,同时也将数据存储在vuex里面
文章来源:https://www.toymoban.com/news/detail-706020.html
文章来源地址https://www.toymoban.com/news/detail-706020.html
到了这里,关于通用vue组件化登录页面的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!