前端自定义指令控制权限(后端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 按钮使用

相关推荐
wuhen_n35 分钟前
网络请求在Vite层的代理与Mock:告别跨域和后端依赖
前端·javascript·vue.js
小彭努力中36 分钟前
193.Vue3 + OpenLayers 实战:圆孔相机模型推算卫星拍摄区域
vue.js·数码相机·vue·openlayers·geojson
用户69371750013846 小时前
Google 正在“收紧侧加载”:陌生 APK 安装或需等待 24 小时
android·前端
蓝帆傲亦6 小时前
Web 前端搜索文字高亮实现方法汇总
前端
用户69371750013846 小时前
Room 3.0:这次不是升级,是重来
android·前端·google
漫随流水7 小时前
旅游推荐系统(view.py)
前端·数据库·python·旅游
踩着两条虫8 小时前
VTJ.PRO 核心架构全公开!从设计稿到代码,揭秘AI智能体如何“听懂人话”
前端·vue.js·ai编程
jzlhll1239 小时前
kotlin Flow first() last()总结
开发语言·前端·kotlin
用头发抵命9 小时前
Vue 3 中优雅地集成 Video.js 播放器:从组件封装到功能定制
开发语言·javascript·ecmascript
蓝冰凌10 小时前
Vue 3 中 defineExpose 的行为【defineExpose暴露ref变量】详解:自动解包、响应性与实际使用
前端·javascript·vue.js