vue按钮权限自定义指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 路由对象
{
    path: 'button-permission',
    component: () => import('@/views/NestedExample/PageButtonPermission'),
    name: 'PageButtonPermission',
    meta: { title: '按钮权限', icon: '按钮权限', buttonPermission: [
        { id: 'search', name: '搜索', permission: true },
        { id: 'add', name: '添加', permission: false },
        { id: 'del', name: '删除', permission: true }
    ] }
}
<!-- 通过自定义指令决定按钮是否显示 -->
<el-button v-button-permission:search type="primary">搜索</el-button>
<el-button v-button-permission:add type="primary">添加</el-button>
<el-button v-button-permission:del type="primary">删除</el-button>
// 自定义指令
Vue.directive('button-permission', {
  bind: (el, { arg }, { context: { $route: { meta }}}) => {
    if (meta.buttonPermission && arg && meta.buttonPermission.find(e => e.id === arg)) {
      if (!meta.buttonPermission.find(e => e.id === arg).permission) {
        el.style.display = 'none'
        Vue.nextTick(() => el.remove())
      }
    }
  }
})

  

posted @   热心市民~菜先生  阅读(239)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2019-03-28 Centos7下安装自动挂载硬盘
2019-03-28 Centos7下安装Nginx文件服务器
2019-03-28 Centos下安装Python3.5
2019-03-28 Centos7下安装FTP文件服务器
点击右上角即可分享
微信分享提示