Vue實現(xiàn)用戶沒有登陸時,訪問后自動跳轉(zhuǎn)登錄頁面的實現(xiàn)思路
設(shè)計思路
- 定義路由的時候配置屬性,這里使用needLogin標記訪問頁面是否需要登錄
- 設(shè)置路由守衛(wèi),每個頁面在跳轉(zhuǎn)之前都要經(jīng)過驗證,校驗用戶信息是否存在,不存在跳轉(zhuǎn)到登錄頁
- 用戶登錄后將用戶信息存儲在localStorage
- 退出登錄后,將用戶信息清空
代碼實現(xiàn)
1、router文件夾的index.js文件中
- 在router中每個地址在meta屬性中配置needLogin熟悉,判斷訪問頁面是否需要登錄
- 404頁面放在最后,匹配所有鏈接,實現(xiàn)輸入不存在的地址時自動跳轉(zhuǎn)404頁面
import Vue from 'vue' import Router from 'vue-router' import LoginCard from "../components/LoginCard"; import Home from "../components/Home"; import ErrorPage from "../components/ErrorPage"; Vue.use(Router) export default new Router({ routes: [ { path: '/', name: 'LoginCard', component: LoginCard, meta: { needLogin: false } }, { path: '/loginCard', name: 'LoginCard', component: LoginCard, meta: { needLogin: false } }, { path: '/home', name: 'Home', component: Home, meta: { needLogin: true } }, { path: '/*', name: 'ErrorPage', component: ErrorPage, meta:{ needLogin: false } } ] })
2、在main.js中定義一個路由前置守衛(wèi),每次跳轉(zhuǎn)頁面進行判斷,沒有登陸自動挑戰(zhàn)登陸界面
import Vue from 'vue' import App from './App' import router from './router' import VueRouter from "vue-router"; import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import * as auth from './utils/auth' import store from './store' import Vuex from "vuex"; Vue.config.productionTip = false; Vue.use(ElementUI); Vue.use(VueRouter); Vue.use(Vuex) //這個方法需要放在new Vue之前,不然按F5刷新頁面不會調(diào)用這個方法 router.beforeEach(function (to, from, next) { console.log('是否需要登錄才能訪問') if (to.meta.needLogin) { if (auth.getAdminInfo()) { console.log(auth.getAdminInfo()) console.log('有cookie信息') next(); }else { console.log('無cookie信息') next({ path:'/loginCard' }); } }else{ next(); } }) new Vue({ el: '#app', router, store, components: { App }, template: '<App/>' })
3、編寫一個存儲數(shù)據(jù)的工具,使用cookie存儲用戶登錄后的信息
import Cookies from 'js-cookie' const adminInfo = "adminInfo" //獲取用戶信息 export function getAdminInfo() { const admin = Cookies.get(adminInfo) if(admin){ return JSON.parse(admin) } return '' } //存儲用戶信息 export function setAdminInfo(admin) { return Cookies.set(adminInfo, JSON.stringify(admin)) } //移除用戶信息 export function removeAdminInfo() { return Cookies.remove(adminInfo) }
4、寫一個登錄頁面,用戶登錄后就將數(shù)據(jù)存儲在cookie中
?<template> <div> <el-form ref="loginForm" :rules="formRules" :model="loginUser" label-width="80px" class="login-box"> <h3 style="margin-bottom: 50px">歡迎登錄</h3> <el-form-item label="用戶名" prop="username"> <el-input prefix-icon="el-icon-user" type="text" v-model="loginUser.username" placeholder="請輸入用戶名" :maxlength="50" clearable></el-input> </el-form-item> <el-form-item label="密碼" prop="password"> <el-input prefix-icon="el-icon-lock" type="password" v-model="loginUser.password" placeholder="請輸入密碼" :maxlength="50" clearable> </el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="onSubmit">登陸</el-button> <el-button icon="" @click="resetForm">重置</el-button> </el-form-item> </el-form> </div> </template> <script> import * as auth from '../utils/auth' export default { name: 'LoginCard', data() { return { loginUser: { username: '', password: '', }, formRules: { //制定表單輸入的規(guī)則 username: [{required: true, message: '用戶名不能為空', trigger: 'blur'}], password: [{required: true, message: '密碼不能為空', trigger: 'blur'}] } } }, methods: { onSubmit() { //判斷表單是否符合規(guī)則 this.$refs['loginForm'].validate((valid) => { if (valid) { if (this.loginUser.username !== '123456' || this.loginUser.password !== '123456'){ this.$message({ message:'賬號或密碼錯誤', type: 'error', }); return; } auth.setAdminInfo(this.loginUser); this.$router.push({path:'/home'}); } } ) }, resetForm(){ this.$refs['loginForm'].resetFields(); }, } } </script> <style scoped> .login-box { border: 1px solid #DCDFE6; width: 400px; margin: 180px auto; padding: 35px 35px 15px 35px; border-radius: 5px; } </style>
5、編寫一個退出頁面,用戶退出以后,將用戶信息從cookie中去除,跳轉(zhuǎn)到登陸頁面
?<template> <div> <h1>主頁面</h1> <el-button @click="logout">退出登錄</el-button> </div> </template> <script> import * as auth from '../utils/auth' export default { name : 'Home', data() { return { }; }, methods: { logout(){ auth.removeAdminInfo(); this.$router.push({path:'/loginCard'}); } }, mounted() { } } </script>
基本目錄結(jié)構(gòu)是這樣的
到此這篇關(guān)于Vue學習:實現(xiàn)用戶沒有登陸時,訪問后自動跳轉(zhuǎn)登錄頁面的文章就介紹到這了,更多相關(guān)Vue自動跳轉(zhuǎn)登錄頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue項目如何保持用戶登錄狀態(tài)(localStorage+vuex刷新頁面后狀態(tài)依然保持)
- vue實現(xiàn)用戶長時間不操作自動退出登錄功能的實現(xiàn)代碼
- vue 實現(xiàn)用戶登錄方式的切換功能
- VuePress 中如何增加用戶登錄功能
- VUE:vuex 用戶登錄信息的數(shù)據(jù)寫入與獲取方式
- vue同一個瀏覽器登錄不同賬號數(shù)據(jù)覆蓋問題解決方案
- vue?+elementui?項目登錄通過不同賬號切換側(cè)邊欄菜單的顏色
- Vue實現(xiàn)登錄記住賬號密碼功能的思路與過程
- vue項目實現(xiàn)表單登錄頁保存賬號和密碼到cookie功能
- vue同一瀏覽器登錄多賬號處理方案
相關(guān)文章
使用watch監(jiān)聽路由變化和watch監(jiān)聽對象的實例
下面小編就為大家分享一篇使用watch監(jiān)聽路由變化和watch監(jiān)聽對象的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02vue2.0 computed 計算list循環(huán)后累加值的實例
下面小編就為大家分享一篇vue2.0 computed 計算list循環(huán)后累加值的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03解決vue 使用setTimeout,離開當前路由setTimeout未銷毀的問題
這篇文章主要介紹了解決vue 使用setTimeout,離開當前路由setTimeout未銷毀的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07