您好,登錄后才能下訂單哦!
本文實例講述了vue路由守衛+登錄態管理。分享給大家供大家參考,具體如下:
在路由文件需要守衛的path后面加上meta
{path: '/home',component: home,meta:{requireAuth:true}}
在main.js里面加上
//路由守衛 router.beforeEach((to, from, next) => { console.log(to); console.log(from); if (to.meta.requireAuth) { // 判斷該路由是否需要登錄權限 if(JSON.parse(localStorage.getItem('islogin'))){ //判斷本地是否存在access_token next(); }else { next({ path:'/login' }) } } else { next(); } /*如果本地 存在 token 則 不允許直接跳轉到 登錄頁面*/ if(to.fullPath == "/login"){ if(JSON.parse(localStorage.getItem('islogin'))){ next({ path:from.fullPath }); }else { next(); } } });
其中islogin是登錄態,就是true or false,true表示登錄,false表示未登錄,存放在localStorage里面,因為localStorage里面只能存字符串,所以存進去的時候需要localStorage.setItem(‘islogin',JSON.stringify(islogin));將islogin變為String類型,取出來的時候需要將islogin轉化為Boolean類型,就比如JSON.parse(localStorage.getItem(‘islogin'))這樣。
那么還有一個問題,就是islogin登錄態的管理,vue不能實時監測localStorage的變化,需要實時監測islogin的變化來在頁面顯示登錄還是已經登錄,我用的是vuex+localStorage來管理islogin。展示部分代碼
//store.js中 import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state:{ //是否登錄判斷 islogin:'' }, mutations:{ login:(state,n) => { //傳入登錄狀態islogin let islogin = JSON.parse(n); localStorage.setItem('islogin',JSON.stringify(islogin)); console.log(islogin); state.islogin = islogin; } } }
在需要改變登錄態的頁面只要觸發上面這個login方法就可以了
//這里是登錄 login() { let flag = true; this.$store.commit('login',flag); this.$router.push("/home"); console.log("登錄成功"); } //這里是退出登錄 exit() { let flag = false; this.$store.commit('login',flag); this.$router.push("/login"); console.log("退出登錄"); }
登錄注冊部分樣式參考的element-ui
登錄注冊
<template> <div class="logReg"> <!-- 登錄 --> <div class="login" v-show="flag"> <div class="login-title">登錄</div> <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="login-ruleForm"> <el-form-item label="賬號" prop="count2"> <el-input type="string" v-model="ruleForm2.count2" placeholder="請輸入您的賬號"></el-input> </el-form-item> <el-form-item label="密碼" prop="pass2"> <el-input type="password" v-model="ruleForm2.pass2" autocomplete="off" placeholder="請輸入您的密碼"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm2('ruleForm2')">提交</el-button> <el-button @click="resetForm2('ruleForm2')">重置</el-button> </el-form-item> </el-form> <div @click="register()" class="toregister" >沒有賬號?<span>立即注冊</span></div> </div> <!-- 注冊 --> <div class="register" v-show="!flag"> <div class="register-title">注冊</div> <el-form :model="ruleForm1" status-icon :rules="rules1" ref="ruleForm1" label-width="100px" class="register-ruleForm"> <el-form-item label="賬號" prop="count1"> <el-input type="string" v-model="ruleForm1.count1" placeholder="請輸入您的賬號"></el-input> </el-form-item> <el-form-item label="密碼" prop="pass1"> <el-input type="password" v-model="ruleForm1.pass1" autocomplete="off" placeholder="請輸入您的密碼"></el-input> </el-form-item> <el-form-item label="確認密碼" prop="checkPass"> <el-input type="password" v-model="ruleForm1.checkPass" autocomplete="off" placeholder="請確認您的密碼"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm1('ruleForm1')">提交</el-button> <el-button @click="resetForm1('ruleForm1')">重置</el-button> </el-form-item> </el-form> <div @click="register()" class="toregister" >已有賬號?<span>立即登錄</span></div> </div> </div> </template>
<script> export default{ name:'logReg', data() { //登錄賬號驗證 var validateCount2 = (rule, value, callback) => { if (value === '') { callback(new Error('請輸入賬號')); } else { if (value != "admin") { callback(new Error('賬號不存在')); } //向后臺請求驗證賬號是否存在 callback(); } }; //登錄密碼驗證 var validatePass2 = (rule, value, callback) => { if (value === '') { callback(new Error('請輸入密碼')); } else { if (value != "admin") { callback(new Error('密碼不正確')); } //向后臺驗證,也可以不處理 callback(); } }; //注冊賬號驗證 var validateCount1 = (rule, value, callback) => { if (value === '') { callback(new Error('請輸入賬號')); } else { //向后臺請求驗證賬號是否重復 callback(); } }; //注冊密碼驗證 var validatePass1 = (rule, value, callback) => { if (value === '') { callback(new Error('請輸入密碼')); } else { if (this.ruleForm1.checkPass !== '') { this.$refs.ruleForm1.validateField('checkPass'); } callback(); } }; //注冊密碼確認 var validatePassCheck = (rule, value, callback) => { if (value === '') { callback(new Error('請再次輸入密碼')); } else if (value !== this.ruleForm1.pass1) { callback(new Error('兩次輸入密碼不一致!')); } else { callback(); } }; return { flag:true,//登錄注冊切換 //登錄賬號密碼 ruleForm2: { pass2: '', count2:'' }, //登錄驗證 rules2: { count2: [ { validator: validateCount2, trigger: 'blur' } ], pass2:[ { validator: validatePass2, trigger: 'blur' } ] }, ruleForm1: { count1: '', pass1: '', checkPass: '' }, rules1: { count1: [ { validator: validateCount1, trigger: 'blur' } ], pass1:[ { validator: validatePass1, trigger: 'blur' } ], checkPass: [ { validator: validatePassCheck, trigger: 'blur' } ] }, }; }, methods: { //登錄提交 submitForm2(formName) { this.$refs[formName].validate((valid) => { if (valid) { console.log("提交成功"); //提交成功之后操作 let flag = true; this.$store.commit("login",flag); this.$router.push('/home'); this.$message({ message: '恭喜,登錄成功', type: 'success' }); } else { this.$message({ message: '抱歉,登錄失敗', type: 'warning' }); return false; } }); }, //登錄框重置 resetForm2(formName) { this.$refs[formName].resetFields(); }, //注冊提交 submitForm1(formName) { this.$refs[formName].validate((valid) => { if (valid) { this.$message({ message: '恭喜,注冊成功,請登錄', type: 'success' }); this.flag = !this.flag; } else { this.$message({ message: '抱歉,注冊失敗', type: 'warning' }); return false; } }); }, //注冊框重置 resetForm1(formName) { this.$refs[formName].resetFields(); }, //切換登錄注冊 register() { this.flag = !this.flag; } }, } </script>
<style scoped> .el-button--primary { color: #fff; background-color: rgba(254, 112, 26, 0.8); border-color: rgba(254, 112, 26, 0.9); } .el-button:focus{ color: #333; background-color: rgba(255, 255, 255, 0.1); border-color: #dcdfe6; } .el-button:hover{ color: rgba(254, 112, 26, 1); background-color: rgba(255, 255, 255, 0.1); border-color: rgba(254, 112, 26, 0.9); } .el-button--primary:hover { color: #fff; background-color: rgba(254, 112, 26, 0.8); border-color: rgba(254, 112, 26, 0.9); } .el-button--primary:focus { color: #fff; background-color: rgba(254, 112, 26, 0.8); border-color: rgba(254, 112, 26, 0.9); } .register, .login{ margin-top: 100px; margin-bottom: 100px; padding: 40px 0px 40px 0; background-color: #fff; width: 600px; margin: 100px auto; border-radius: 8px; box-shadow: 0px 0px 100px rgba(0,0,0,.1); } .register .register-title, .login .login-title{ font-size: 1.65rem; line-height: 60px; font-weight: bold; white-space: nowrap; margin-bottom: 16px; color: #555; /* color: rgba(254, 112, 26, 0.8);*/ } .register-ruleForm, .login-ruleForm{ width: 500px; margin: 0 auto; padding: 0px 100px 0px 0; } .login .toregister{ cursor: pointer; } /*注冊*/ </style>
希望本文所述對大家vue.js程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。