Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

退出登录后再次重新登录会报错 #27

Closed
xwhuxx opened this issue Jun 18, 2020 · 8 comments
Closed

退出登录后再次重新登录会报错 #27

xwhuxx opened this issue Jun 18, 2020 · 8 comments

Comments

@xwhuxx
Copy link

xwhuxx commented Jun 18, 2020

报错信息如下:"Uncaught (in promise) Error: Redirected from "/login" to "/home" via a navigation guard"
如果主页有从后端请求的数据也会无法进行获取。

@woai3c
Copy link
Owner

woai3c commented Jun 18, 2020

permission.js 的代码发一下

@xwhuxx
Copy link
Author

xwhuxx commented Jun 18, 2020

这部分我应该没怎么改动
import { LoadingBar } from 'view-design'
import createRoutes from '@/utils/createRoutes'
import router from './router'
import store from './store'
import { getDocumentTitle, resetTokenAndClearUser } from './utils'

// 是否有菜单数据
let hasMenus = false
router.beforeEach(async (to, from, next) => {
document.title = getDocumentTitle(to.meta.title)
LoadingBar.start()
if (localStorage.getItem('token')) {
if (to.path === '/login') {
next({ path: '/' })
} else if (hasMenus) {
next()
} else {
try {
const routes = createRoutes(store.state.menuItems)
router.addRoutes(routes)
hasMenus = true
next({ path: to.path || '/' })
} catch (error) {
resetTokenAndClearUser()
next(/login?redirect=${to.path})
}
}
} else {
hasMenus = false
if (to.path === '/login') {
next()
} else {
next(/login?redirect=${to.path})
}
}
})

router.afterEach(() => {
LoadingBar.finish()
})

@woai3c
Copy link
Owner

woai3c commented Jun 18, 2020

发代码的时候把代码放在两个 ``` 中间。

```
test
```

类似这样

@xwhuxx
Copy link
Author

xwhuxx commented Jun 18, 2020

不好意思,比较少提issue,不大清楚,我比较了下你这边的,应该只是我把import createroute提前了,因为我这边如果不提前他就会报错

import { LoadingBar } from 'view-design'
import createRoutes from '@/utils/createRoutes'
import router from './router'
import store from './store'
import { getDocumentTitle, resetTokenAndClearUser } from './utils'

// 是否有菜单数据
let hasMenus = false
router.beforeEach(async (to, from, next) => {
document.title = getDocumentTitle(to.meta.title)
LoadingBar.start()
if (localStorage.getItem('token')) {
if (to.path === '/login') {
next({ path: '/' })
} else if (hasMenus) {
next()
} else {
try {
const routes = createRoutes(store.state.menuItems)
router.addRoutes(routes)
hasMenus = true
next({ path: to.path || '/' })
} catch (error) {
resetTokenAndClearUser()
next(/login?redirect=${to.path})
}
}
} else {
hasMenus = false
if (to.path === '/login') {
next()
} else {
next(/login?redirect=${to.path})
}
}
})

router.afterEach(() => {
LoadingBar.finish()
})

@woai3c
Copy link
Owner

woai3c commented Jun 18, 2020

这个看着没问题,看一下路由文件,估计是重定向导致的。

@xwhuxx
Copy link
Author

xwhuxx commented Jun 18, 2020

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const commonRoutes = [
    {
        path: '/login',
        name: 'login',
        meta: { title: '登录' },
        component: () => import('../components/Login.vue'),
    },
    {
        path: '/404',
        name: '404',
        meta: { title: '404' },
        component: () => import('../components/404.vue'),
    },
    { path: '/', redirect: '/home' },
]

// 本地所有的页面 需要配合后台返回的数据生成页面
export const asyncRoutes = {
    home: {
        path: '/home',
        name: 'home',
        meta: { title: '主页' },
        component: () => import('../views/Home.vue'),
    },
    messagelist: {
        path: '/messagelist',
        name: 'messagelist',
        meta: { title: '信息展示' },
        component: () => import('../views/messagelist.vue'),
    },
}

const createRouter = () => new Router({
    routes: commonRoutes,
})

const router = createRouter()

export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher
}

export default router

@xwhuxx
Copy link
Author

xwhuxx commented Jun 18, 2020

补充下store信息

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        // 左侧菜单栏数据
        menuItems: [
            {
                name: 'home',
                text: '主页',
                size: 18,
                type: 'md-home',
            },
            {
                size: 18,
                text: '主机信息',
                type: 'md-desktop',
                children: [
                    {
                        size: 18,
                        type: 'ios-grid',
                        name: 'messagelist',
                        text: '信息展示',
                    },
                ],
            },
        ],
    },
    mutations: {
        setMenus(state, items) {
            state.menuItems = [...items]
        },
    },
})

@woai3c
Copy link
Owner

woai3c commented Jun 19, 2020

{ path: '/', redirect: '/home' },

这行代码有问题,把它放到异步路由里。

@woai3c woai3c closed this as completed Jun 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants