Vue3.0+vue-router 实现权限路由方案一

  • 定义路由

const routes=[
    {
    path: '/page',
    name: "dashboard",
    redirect: '/page/home',
    meta: {
      title: "首页",
    },
    component: () => import('@/components/layout/index.vue'),
    children: [
      {
        path: 'home',
        name: "home",
        meta: {
          title: "仪表盘",
          role: [1,2,3,4,5]
        },
        component: () => import("@/pages/home/index.vue"),
      },
      {
        path: 'application-form-list',
        name: "application-form-list",
        redirect: '/page/application-form-list/application-list',
        meta: {
          title: "申请表列表",
          role: [2]
        },
        component: () => import("@/components/childView.vue"),
        children: [
          {
            path: 'application-list',
            name: "application-list",
            meta: {
              title: "",
              role: [1,2,3]
            },
            component: () => import("@/pages/applicationFormList/index.vue"),
          },
          {
            path: 'application-history',
            name: "application-history",
            meta: {
              title: "历史申请",
              role: [2]
            },
            component: () => import("@/pages/applicationFormList/historyList.vue"),
          }
        ]
      }
    ]
]

​​​​​​​role:定义拥有该页面权限的用户

  • 创建路由

const router = createRouter({ history:'...',routes})
  • 创建导航守卫 

router.beforeEach(async(to, from, next) => {
  if (to.path !== '/login') {
    const userInfo=xxx;//用户数据
    const menuList=[...]//菜单、路由数组
    const role=xxx//用户权限
    const hasRole=to.meta.role.indexOf(role)
    if(hasRole==-1){//无访问权限
      if(to.path=="/page/home"&&menuList.length>0){//首页没有权限重定向
        return next(menuList[0].path)
      }else{//跳转无权限页面
        return next('/page/permission')
      }
    }

    if(!userInfo) {
      return next('/login')
    }
  }
  
  next()
})

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值