中后台管理项目-button按钮的权限控制

 1、下面是json格式的关于教师登录要展示的路由配置

[
    {
        "path": "/project",
        "name": "project",
        "title": "项目管理",
        "component": "/Layout/index.vue",
        "redirect": "/project/add",
        "children": [
            {
                "path": "/project/add",
                "title": "项目管理",
                "component": "/Project-M/add.vue"
            },
            {
                "path": "/project/info",
                "title": "项目审核",
                //动态添加权限
                "rights": [
                    "view"
                    
                ],
                "component": "/Project-M/Info.vue"
            },
            {
                "path": "/project/operation",
                "title": "项目操作日志列表",
                "component": "/Project-M/Operation.vue"
            }
        ]
    },
    {
        "path": "/Social-security",
        "name": "Social-security",
        "title": "社保管理",
        "component": "/Layout/index.vue",
        "redirect": "/Social-security/Query",
        "children": [
            {
                "path": "/Social-security/Query",
                "title": "社保查询",
                "component": "/Social-security/Query.vue"
            },
            {
                "path": "/Social-security/upload",
                "title": "社保上传",
                "component": "/Social-security/Upload.vue"
            }
        ]
    }
]

2. 自定义权限校验的指令

/src/directives/permission.js

import router from '@/router'
export default {
    mounted(el,binding){
      //bindling.value为指令的绑定值
      const action = binding.value.action 
      const currentRight = router.currentRoute.value.meta.rights
      if(currentRight){
        //假设某用户对某模块只有添加和删除的权限
        //这个权限信息应该是不同用户登录时从后台拿到的对应的信息
        if (currentRight.indexOf(action) === -1){
            //不具备权限
            const type = binding.value.effect
            if (type === 'disabled'){
                //禁用
                el.disabled = true
                el.classList.add("is-disabled")
            }else{
                //移除
                el.parentNode.removeChild(el)
            }
        }
      }
    }
  }

 /src/directives/index.js

import permission from "./permission"
//批量注册指令(现在就一个permission)
const directives = {
  permission
}
//注册的一般写法,循环遍历directives,通过vue.directive注册
export default {
  install(Vue){
    Object.keys(directives).forEach(key=>{
      console.log("zcccc")
      Vue.directive(key,directives[key])
    })
  }
}

全局注册

import Directives from "./directives/index"
app.use(Directives)

班级管理中的添加按钮

学生管理中的删除按钮

  <button v-permission="{action:'add', effect:'disabled'}">添加班级</button>
   <button v-permission="{action:'delete'}">删除</button>

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值