VUE腳手架框架編寫簡潔的登錄界面的實(shí)現(xiàn)
前言
一個好的前端開發(fā)項(xiàng)目,都是一個團(tuán)隊負(fù)責(zé)一個部分進(jìn)行通力合作的。簡單的一個系統(tǒng)網(wǎng)站一般包含登錄、主體、各個模塊功能這三個大部分,現(xiàn)在我們寫的登錄界面,我們一般編寫這樣的登錄界面可以說有一定的固定套路。
安裝Vue腳手架
1、我們一Windows 10為例子,安裝Vue 3腳手架前,我們先要配置node js環(huán)境;
2、接下來,安裝Vue,安裝的過程不多說,大家可以到官網(wǎng)去自行下載就可以了,鏈接Vue.js;Vue.js - The Progressive JavaScript
或者,可以選擇另一種安裝方式:
打開控制臺,輸入一下命令行
npm install vue -g
3、等待安裝Vue,還需安裝Vue-cli (腳手架),輸入如下命令進(jìn)行安裝
npm install -g vue-cli
4、對項(xiàng)目文件進(jìn)行初始化 還可以使用webpack作為腳手架,命令如下:
vue init webpack qiubite-project
創(chuàng)建登錄界面的文件--login.Vue
安裝完畢后,這里本人使用的是vscode軟件,創(chuàng)建一個登錄模塊的目錄頁,

配置路由-- router.js
接下來,我們要把vue的router路由配置給配置好,如下:
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
Vue.use(VueRouter)
const router = new VueRouter({
routes: [
{ path: '/', redirect: '/login' },
{ path: '/login', component: Login },
{
path: '/home',
component: Home,
redirect: '/equipment', // 重定向功能,只要訪問home地址,就會重定向到wequipment地址
children: [
{ path: '/equipment', component: Equip },
{ path: '/deploy', component: Deploy },
{ path: '/history', component: History },
{ path: '/analysis', component: Analysis },
{ path: '/power', component: Power },
{ path: '/remote', component: Remote }]// welcome為home的子屬性
}
],
mode: 'history'
})
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這里我們掛載一個路由守衛(wèi),router.beforeEach,這里面to將要訪問的路徑,from 代表從哪一個路徑跳到哪一個去beforeEach,next 是一個函數(shù),表示放行,next()放行 next('/login)強(qiáng)制跳轉(zhuǎn)。用戶如果訪問login,可以直接跳轉(zhuǎn),獲取token。
配置main.js
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// 導(dǎo)入全局樣式表
import './assets/css/global.css'
import axios from 'axios'
axios.defaults.baseURL = 'http://39.101.161.112:9000'
axios.interceptors.request.use(config => {
console.log(config)
config.headers.Authorization = window.sessionStorage.getItem('token')
// 通過axios攔截器添加token驗(yàn)證
return config
// 最后必須要return出去
})
Vue.prototype.$http = axios
Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.use(AMap)
Vue.use(VideoPlayer)
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
編寫登錄界面--Login.Vue
<template>
<div class="login_container">
<!-- 登錄模塊盒子 -->
<div class="login-box">
<!-- 圖片盒子 -->
<div class="avatar_box">
<img src="../assets/wo.jpg" alt="">
<h3 class="text">丘 比 特</h3>
</div>
<!-- 登陸表單區(qū)域 -->
<el-form label-width="0px" class="login_form" :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
<!-- :model數(shù)據(jù)綁定對象,綁定到login from這個表單中,rules表單的驗(yàn)證規(guī)則對象 -->
<!-- 用戶登錄 -->
<el-form-item prop="username">
<el-input prefix-icon="el-icon-user-solid" v-model="loginForm.username" size="mini" ></el-input>
<!-- v-model雙向綁定,綁定到用戶名 -->
</el-form-item>
<!-- 密碼 -->
<el-form-item prop="password">
<el-input prefix-icon="el-icon-lock" v-model="loginForm.password" type="password" size="mini" ></el-input>
<!-- v-model雙向綁定,綁定到用戶登錄密碼,type="password可以使得密碼隱藏"-->
</el-form-item>
<!-- 按鈕區(qū)域 -->
<el-form-item class="btns">
<el-button type="primary" @click="login" size="mini" >登錄</el-button>
<!-- 為登錄綁定一個單擊事件,名為login -->
<el-button type="info" @click="resetLoginForm" size="mini">取消</el-button>
<!-- click綁定單擊事件,名為resetloginfrom -->
</el-form-item>
</el-form>
</div>
</div>
</template>
<script>
import {Login} from '../network/login'
export default {
data () {
return {
loginForm: {
username: '',
password: ''
},
loginFormRules: {
username: [
{ required: true, message: '請輸入登錄名稱', trigger: 'blur' },
{ min: 3, max: 10, message: '長度在 3 到 10 個字符', trigger: 'blur' }
],
password: [
{ required: true, message: '請輸入登錄密碼', trigger: 'blur' },
{ min: 3, max: 15, message: '長度在 3 到 15 個字符', trigger: 'blur' }
]
}
}
},
methods: {
resetLoginForm () {
this.$refs.loginFormRef.resetFields()
},
login () {
this.$refs.loginFormRef.validate( valid => {
if (!valid) {return this.$message.error('用戶名密碼不正確')}
Login(this.loginForm).then( res => {
console.log(res);
if (res.code !== 0)
{return this.$message.error('登錄失敗')}
this.$message.success('登陸成功')
window.sessionStorage.setItem('token', res.token)
this.$router.push('/home')
}).catch( error => {
console.log(error);
})
})
}
}
}
</script>
<style lang="less" scoped>
.login_container {
background-color: #0e1729;
background-image: url(../assets/pic2.png);
height: 100%;
}
.login-box {
width: 360px;
height: 240px;
background-color: #fff;
border-radius: 3px;
position: absolute;
left: 50%; //距離左側(cè)50%
top: 50%; // 距離頂部505
transform: translate(-50%, -50%); //橫軸上移動50%,縱移動50%
}
.avatar_box {
height: 100px;
width: 100px;
border: 1px solid #eee;
border-radius: 50%;
padding: 10px;
box-shadow: 0 0 10px #eee;
position: absolute;
left: 50%;
top:-40%;
transform: translate(-50%);
background-color: rgb(32, 181, 201);
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;
}
.btns {
display: flex;
justify-content: flex-end;
}
.text {
text-align: center;
margin-top: 10px;
font-size: 20px;
font-family: 宋體;
}
</style>1、 validate回調(diào)函數(shù),完成登錄表單前的預(yù)校驗(yàn);
2、將登陸成功以后的token,保存到客戶端的sessionstorage中;
3、項(xiàng)目中除了登陸之外的其他api接口,必須將token保存到客戶端;
4、 this.$router.push('/home)是通過編程式導(dǎo)航跳轉(zhuǎn)到后臺,路由地址為/home。
結(jié)果展示

總結(jié)
用戶登錄首先完成賬號密碼輸入,當(dāng)用戶進(jìn)行賬戶密碼登錄操作,系統(tǒng)將驗(yàn)對賬戶密碼進(jìn)行查詢、登錄表單數(shù)據(jù)綁定驗(yàn)證。當(dāng)用戶登錄成功時,用戶信息將會被記錄到session中,用戶信息將會被記錄到系統(tǒng)日志中,再通過重定向即可跳轉(zhuǎn)到系統(tǒng)首頁。當(dāng)用戶登錄失敗時,系統(tǒng)彈出“登陸失敗,請重新登錄”提示信息。當(dāng)用戶登錄賬戶密碼不合法時,系統(tǒng)將彈出“登錄信息不合法”提示信息,用戶需進(jìn)行賬戶密碼重置重新登陸。如下是用戶登錄界面的時序圖:

到此這篇關(guān)于VUE腳手架框架編寫簡潔的登錄界面的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)VUE 登錄界面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項(xiàng)目中常見的三種文件類型在線預(yù)覽實(shí)現(xiàn)(pdf/word/excel表格)
最近在項(xiàng)目中要做一個pdf在線預(yù)覽的功能,索性給大家整理個全面的,這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目中常見的三種文件類型在線預(yù)覽實(shí)現(xiàn)的相關(guān)資料,文件類型分別是pdf/word文件/excel表格,需要的朋友可以參考下2022-05-05
electron中使用本地數(shù)據(jù)庫的方法詳解
眾所周知,electron是可以開發(fā)桌面端的框架,那我們有一些數(shù)據(jù)不想讓別人看到,只能在自己的電腦上展示時怎么辦呢,這個時候就可以用到本地數(shù)據(jù)庫,本文將以sqlite3為例介紹一下electron如何使用本地數(shù)據(jù)庫2023-10-10
vue.js實(shí)現(xiàn)刷新當(dāng)前頁面的方法教程
這篇文章主要給大家介紹了關(guān)于vue.js實(shí)現(xiàn)刷新當(dāng)前頁面的方法教程,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2017-07-07
vue刷新頁面時去閃爍提升用戶體驗(yàn)效果的實(shí)現(xiàn)方法
這篇文章主要介紹了vue刷新頁面時去閃爍提升體驗(yàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-12-12
vue實(shí)現(xiàn)公告欄文字上下滾動效果的示例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)公告欄文字上下滾動效果的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
vue組件傳遞對象中實(shí)現(xiàn)單向綁定的示例
下面小編就為大家分享一篇vue組件傳遞對象中實(shí)現(xiàn)單向綁定的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02

