• 路由权限配置


    router/index.js

    import Vue from 'vue'
    import Router from 'vue-router'
    import Login from '@/pages/login'
    import { generateRoutes } from './permission'
    Vue.use(Router)
    
    export const routes = [
        {
            path: '/login',
            name: 'login',
            hidden: true,
            component: Login
        },
        {
            path: '/',
            redirect: '/Choose'
        },
        {
            path: '/Choose',
            name: 'choose',
            component: () => import('../pages/choose')
        },
        {
            path: '/Use',
            name: 'Use',
            component: () => import('../pages/use')
        },
        {
            path: '/Sdk',
            name: 'sdk',
            component: () => import('../pages/sdk/sdk')
        },
        {
            path: '/404',
            name: '404页面',
            component: () => import('../pages/404.vue')
        }
    ]
    
    // 需根据权限进行动态生成的路由
    export const asyncRoutes = [
        { path: '/test1', meta: { roles: ['admin'] }, name: 'test1', component: () => import('../pages/test1') },
        { path: '/test2', meta: { roles: ['edit'] }, name: 'test2', component: () => import('../pages/test2') },
        { path: '/test3', meta: { roles: ['admin', 'edit'] }, name: 'test3', component: () => import('../pages/test3') },
        { path: '*', redirect: '/404', hidden: true }
    ]
    
    const router = new Router({
        routes: routes
    })
    
    var flag = true // 页面刷新标志
    
    router.beforeEach((to, from, next) => {
        if (to.path === '/login') {
            sessionStorage.removeItem('name')
            // sessionStorage.removeItem('pass')
            flag = true
            next()
        }
        let admin = sessionStorage.getItem('name')
        if (!admin && to.path !== '/login') {
            console.log('不是登录页面')
            next({ path: '/login' })
        } else {
            // 该处进行动态路由生成(当页面刷新时或首次进入该系统时,生成路由)
            if (flag && to.path !== '/login') {
                flag = false
                router['options'].routes = routes
                let generateAsyncRoutes = generateRoutes(asyncRoutes, sessionStorage.getItem('name')) // 根据登录角色生成动态路由
                router.addRoutes(generateAsyncRoutes)
                router['options'].routes = router['options'].routes.concat(generateAsyncRoutes)
                next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
            } else {
                next()
            }
        }
    })
    
    export default router
    
    

    router/permission.js

    function hasPermission(route, role) {
        if (route.meta && route.meta.roles) {
            return route.meta.roles.indexOf(role) !== -1
        } else {
            return true
        }
    }
    
    // 该处的role为登录角色,可根据实际需要进行动态生成
    export const generateRoutes = function(asyncRoutes, role) {
        const res = []
        asyncRoutes.forEach(item => {
            const mid = { ...item }
            if (hasPermission(mid, role)) {
                if (mid.children) {
                    mid.children = generateRoutes(mid.children, role) // 对子路由进行递归,防止遗漏(同时该部分支持多级子路由的递归生成)
                }
                res.push(mid)
            }
        })
        return res
    }
    
    

    login.vue

    <template>
        <div>
            <div>登陆页面</div>
            <div @click="login">登陆</div>
        </div>
    </template>
    <script>
    export default {
        methods: {
            login() {
                sessionStorage.setItem('name', 'admin')
                this.$router.push('/choose')
            }
        }
    }
    </script>
    
    
  • 相关阅读:
    Android防止手动添加的本地库文件被NDK工具清理掉
    将驱动编译进Linux内核
    cocos2d-x入门学习笔记——Hello world分析
    linux内核开发入门学习
    makefile工程管理
    GDB程序调试工具
    ios学习笔记_20140308
    Mac Os学习笔记-下载黑屏
    时间过得好快
    做一个关于预防接种的app
  • 原文地址:https://www.cnblogs.com/yzyh/p/12709772.html
Copyright © 2020-2023  润新知