参考文章:若依框架登录后跳转其他页面&获取不同的菜单&登录进入后跳转至动态路由的第一个路由
需求:登录成功,默认跳转至后端返回的动态路由的第一个路由
隐藏首页
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
})