vue路由守卫+登录态管理实例分析
程序员文章站
2023-12-16 12:41:52
本文实例讲述了vue路由守卫+登录态管理。分享给大家供大家参考,具体如下:
在路由文件需要守卫的path后面加上meta
{path: '/home',comp...
本文实例讲述了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程序设计有所帮助。
推荐阅读