前端自定义指令控制权限(后端Spring Security)

1. 新建 directives/auth.ts

javascript 复制代码
//导入自定义指令
import auth from '@/directives/auth'// 注册全局自定义指令 v-auth
app.directive('auth', auth);

1.1完整的authDirective.ts

javascript 复制代码
import { wmsStore } from "@/store/pinia"

// 判断用户是否有某个角色的函数
const hasRoles = (roles: any) => {

  const pinaRoles: any = wmsStore().roles;

  if (Array.isArray(roles)) {
    return roles.some(role => pinaRoles.includes(role));
  } else if (typeof roles === 'string') {
    return pinaRoles.includes(roles);
  } else {
     return false
  }

}
// 判断用户是否有某个权限的函数,同上
const hasPermissions = (permission: any) => {

  return true
}

// 创建自定义指令
export default {
  mounted(el: HTMLElement, binding: any) {
 
    const type = binding.arg
    if (type === 'role') {
      if (!hasRoles(binding.value)) {
        el.remove()
      }
    }
  },
};
  

2.如何使用

2.1 菜单使用方式

2.2 按钮使用

相关推荐
你不是我我29 分钟前
【Java 开发日记】SQL 语句左连接右连接内连接如何使用,区别是什么?
java·javascript·数据库
一壶浊酒..1 小时前
请求签名(Request Signature)
javascript
P***25391 小时前
前端构建工具缓存清理,npm cache与yarn cache
前端·缓存·npm
好奇的菜鸟1 小时前
解决 npm 依赖版本冲突:从 “unable to resolve dependency tree“ 到依赖管理高手
前端·npm·node.js
lcc1871 小时前
Vue 内置指令
前端·vue.js
lijun_xiao20092 小时前
前端React18入门到实战
前端
o***Z4482 小时前
前端响应式设计资源,框架+模板
前端
IT_陈寒2 小时前
Vue 3.4 正式发布:5个不可错过的性能优化与Composition API新特性
前端·人工智能·后端
N***73852 小时前
前端无障碍开发资源,WCAG指南与工具
前端
Cocktail_py3 小时前
JS如何调用wasm
开发语言·javascript·wasm