若依 ruoyi-vue 获取上一页路由 获取返回上一页路径 登录后跳转其他页面 登录进入后跳转至动态路由的第一个路由
参考文章:若依框架登录后跳转其他页面&获取不同的菜单&登录进入后跳转至动态路由的第一个路由
需求:登录成功,默认跳转至后端返回的动态路由的第一个路由
隐藏首页可见
src/router/index.js
hidden: true,

保存第一个路由
src/store/modules/permission.js
将动态路由的第一个路由存到缓存中

import cache from '@/plugins/cache'let defaultPage='/index'//修改默认页面if (res.data[0] && res.data[0].children[0] && res.data[0].children[0].children[0]) {defaultPage = res.data[0].path + '/' + res.data[0].children[0].path + '/' + res.data[0].children[0].children[0].path} else if (res.data[0] && res.data[0].children[0]) {defaultPage = res.data[0].path + '/' + res.data[0].children[0].path}cache.session.set('defaultPage', defaultPage)
src/permission.js
存储上一路由,防止重复跳转

import cache from '@/plugins/cache'cache.session.set('prePath', from.path)
方案一,还保留首页,在首页自动跳转,改动地方太多了,可能漏改,我是首页,第一个路由都可跳转
src/views/index.vue
gotoMenu() {const prePath = this.$cache.session.get('prePath')const defaultPage = this.$cache.session.get('defaultPage')let permi = checkPermi(['train:trainingrelease:list'])if (permi) {if (prePath !== '/train/training/trainingrelease') {this.$router.push({ path: '/train/training/trainingrelease' }).catch(() => {})}} else if (prePath !== defaultPage) {//如果上一页是默认页面,则不跳转到默认页面this.$router.push({ path: defaultPage }).catch(() => {})}},
方案二:不保留首页
目前我知道用到首页的地方,401、404、登录完成跳转
src/views/error/401和404
<template><div class="errPage-container"><el-button icon="arrow-left" class="pan-back-btn" @click="back">返回</el-button><el-row><el-col :span="12"><h1 class="text-jumbo text-ginormous">401错误!</h1><h2>您没有访问权限!</h2><h6>对不起,您没有访问权限,请不要进行非法操作!您可以返回主页面</h6><ul class="list-unstyled"><li class="link-type"><router-link :to="path">回首页</router-link></li></ul></el-col><el-col :span="12"><img :src="errGif" width="313" height="428" alt="Girl has dropped her ice cream."></el-col></el-row></div>
</template><script>
import errGif from '@/assets/401_images/401.gif'export default {name: 'Page401',data() {return {errGif: errGif + '?' + +new Date(),path: this.$cache.session.get('defaultPage')}},methods: {back() {if (this.$route.query.noGoBack) {this.$router.push({ path: this.path })} else {this.$router.go(-1)}}}
}
</script><style lang="scss" scoped>.errPage-container {width: 800px;max-width: 100%;margin: 100px auto;.pan-back-btn {background: #008489;color: #fff;border: none!important;}.pan-gif {margin: 0 auto;display: block;}.pan-img {display: block;margin: 0 auto;width: 100%;}.text-jumbo {font-size: 60px;font-weight: 700;color: #484848;}.list-unstyled {font-size: 14px;li {padding-bottom: 5px;}a {color: #008489;text-decoration: none;&:hover {text-decoration: underline;}}}}
</style>
<router-link :to="path" class="bullshit__return-home">返回首页</router-link>
data() {return {path: this.$cache.session.get('defaultPage')}},
登录页
this.$store.dispatch('Login', this.loginForm).then(() => {this.loading = falselet defaultPage = this.$cache.session.get('defaultPage')this.$router.push({ path: this.redirect || defaultPage || '/index' }).catch(() => {})}).catch(() => {this.loading = false})
