Разрешение Router.beforeEach не отображать другие страницы - PullRequest
0 голосов
/ 27 мая 2020

я новенький ie. я хотел отобразить другие страницы в моем проекте vuejs. Однако у меня есть разрешение. js всякий раз, когда я комментирую router.beforeEach ((to, from, next))}; отображаются другие страницы. но когда я раскомментирую его, другие страницы не отображаются. Я не знаю, почему это не отображается. Я пытаюсь найти, но безуспешно. Кто-нибудь может мне помочь? Спасибо

Вот код.

разрешение. js

import router from './router'
import store from './store'
import { getToken } from '@/utils/auth' 


function hasPermission(roles, permissionRoles) {
    if (roles.includes('admin')) return true 
    if (!permissionRoles) return true
    return roles.some(role => permissionRoles.indexOf(role) >= 0)
}

const whiteList = ['/signin', '/home', '/auth-redirect']
    router.beforeEach((to, from, next) => {
        if (getToken()) {
            if (to.path === '/signin') {
                next({ path: '/' })
            } else {
                if (store.getters.roles.length === 0) {
                    store.dispatch('GetInfo').then(res => {
                        const data = res.data.data
                        const roles = data.roles

                    // load statics
                    store.dispatch('LoadStaticOptions')
                    store.dispatch('LoadDynamicOptions')

                    store.dispatch('GenerateRoutes', { roles }).then(accessRoutes => {
                        router.addRoutes(accessRoutes)
                        router.options.routes = router.options.routes.concat(accessRoutes)

                        next({...to, replace: true })
                    })
                }).catch((err) => {
                    store.dispatch('LogOut').then(() => {
                        next({ path: '/' })
                    })
                })
            } else {
                if (hasPermission(store.getters.roles, to.meta.roles)) {
                    next()
                } else {
                }
            }
        }
    } else {
        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            next(`/signin?redirect=${to.path}`)
        }
    }
})

router.afterEach(() => {
})

index. js

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

Vue.use(Router)
export const constantRouterMap = [{
        path: '/signin',
        component: () =>
            import ('./views/login/signin')
    },
    {
        path: '/home',
        component: () =>
            import ('./views/home/home')
    }    ]

export default new Router({
    scrollBehavior: () => ({
        y: 0
    }),
    routes: constantRouterMap
})

export const asyncRoutes = []
...