若依 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、登录完成跳转 搜索 to=“/”

src/views/error/401和404
        <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 = false
        let defaultPage = this.$cache.session.get('defaultPage')
        this.$router.push({ path: this.redirect || defaultPage || '/index' }).catch(() => {
        })
      }).catch(() => {
        this.loading = false
      })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值