vue 某些页面要登录之后才能访问,且登录之后跳转到之前的页面

您所在的位置:网站首页 点击登录查看 vue 某些页面要登录之后才能访问,且登录之后跳转到之前的页面

vue 某些页面要登录之后才能访问,且登录之后跳转到之前的页面

2024-07-11 14:41| 来源: 网络整理| 查看: 265

// router文件夹下的 index.js import Vue from "vue"; import Router from "vue-router"; import routes from "./routes"; // 配置的路径 Vue.use(Router); const router = new Router({ routes, scrollBehavior // mode: 'history' }); const router = new Router({ routes, scrollBehavior // mode: 'history' }); router.beforeEach((to, from, next) => { if (to.matched.some(route => route.meta && route.meta.requiresAuth)) { if (!this.$storage.getStorage("userInfo")) { // 没有登录信息跳转到登录页 next({ path: "/login", query: { redirect: to.fullPath } // 'to.fullPath'跳转到登录之前页面的路径 }); } else { next(); } } else { next(); } }); export default router;

下方是我微信公众号的二维码,可以扫码关注以下,后期博文推送主要在公众号上面,有什么问题也可以通过公众号跟我发消息哦~ 在这里插入图片描述

在登陆页面( login.vue里 )

提交 import { webLogin } from '@/apis/apis' export default { data() { return { username: '', password: '', } }, created() { }, methods: { onSubmit() { if (!this.username) { this.$notify({ type: "danger", message: "请填写用户名" }); return; } else if (!this.password) { this.$notify({ type: "danger", message: "请填写密码" }); return; } let params = { username: this.username, password: this.password } webLogin(params).then(res => { if (res.code == 0) { this.$notify({ type: 'success', message: '恭喜,登录成功!' }); // this.$router.push('/user'); this.$storage.setStorage('userInfo', res.data); setTimeout(() => { let path = '/user'; // '/user' 为个人中心页面 if (this.$route.query.redirect) { path = this.$route.query.redirect // 跳到之前的页面 } this.$router.push({ path: path }); }, 1000) } else { this.$notify(res.message) } }) } } }

在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3