Vue3实现按钮级别权限的几种方式

按钮级别的权限管理

1、通过公用的hasPermission方法配合v-if、v-show指令,或者disbaled属性控制按钮的是否渲染或禁用

​ 实现按钮级别的权限有很多方法,比如:在vue中可以封装一个公共的方法,将该按钮所需的权限与该用户拥有的权限进行比较,若该用户的权限码列表包含该按钮的所需权限码,则该方法返回ture,否则返回false。

例如:

<van-button type="success" v-if="hasPermission(["1001", "1002"])">新增</van-button>

通过hasPermission传入的按钮所需权限,进行判断该用户是否有该按钮的权限,是否渲染该按钮。

用户登录成功之后,后端会返回该用户的token、按钮权限码列表,再将这些信息存入store中。

hasPermission方法,首先从store中获取该用户的权限码,与该按钮所需的权限码做对比。

function hasPermission(requiredPermissions) {
  // 假设当前用户的权限存储在变量 userPermissions 中
  const userPermissions = ["1001", "1002", "1003"];

  // 检查用户权限是否包含所需权限
  for (const permission of requiredPermissions) {
    if (!userPermissions.includes(permission)) {
      return false;
    }
  }

  // 所有所需权限都存在于用户权限中
  return true;
}

使用ts封装

// permissionUtil.ts
import { useUserStore } from "@/store/user"

// import { PermissionCode } from "@/types/data"
export type PermissionCode = "1001" | "1002" | "1003"

export const hasPermission = (btnPermissions: PermissionCode | PermissionCode[]) => {
    // 用户的权限码
    const userStore = useUserStore()
    const userPermission = userStore.permission as PermissionCode[]

    // 按钮所需单个权限code
    if (!Array.isArray(btnPermissions)) {
        return userPermission.indexOf(btnPermissions) !== -1
    }

    // 按钮所需多个权限code
    for (const btnPermissionCode of btnPermissions) {
        if (!userPermission.includes(btnPermissionCode)) {
            return false;
        }
    }

    return true
}

2、自定义指令的方式

通过公用的hasPermission方法配合自定义指令(v-isAuth)配置

<van-button v-isAuth="['1001', '1002']" type="default">默认按钮</van-button>
// main.ts
app.directive("isAuth", {
    mounted(el: HTMLElement, binding){
        // 按钮所需权限码
        const btnCodes = binding.value
        const isAuth = hasPermission(btnCodes)

        // 无权限
        if(!isAuth){
            el.parentNode?.removeChild(el)
        }
        
    }
})

3、插件的方式

如果觉得在main.ts中注册全局指令不够优雅,也可以使用插件的方式,将逻辑单独放在一个ts文件里

<van-button v-isAuth="['1001', '1002']" type="default">默认按钮</van-button>
// btnPermissionDirective.ts
import { App, DirectiveBinding } from 'vue';
import { hasPermission } from '@/utils/permissionUtil';
import { PermissionCode } from "@/types/data"

const isAuthDirective = {
    mounted(el: HTMLElement, binding: DirectiveBinding<string[]>) {

        const btnCodes = binding.value as PermissionCode[];

        const isAuth = hasPermission(btnCodes);

        if (!isAuth) {
            el.parentNode?.removeChild(el);
        }
    }
};
// 这里会默认导出一个 isAuthDirective方法
export default {
    install(app: App, options: any) {
        app.directive('isAuth', isAuthDirective);
    }
};

在main.ts中使用

// main.js
import isAuthDirective from '@/directives/btnPermissionDirective';

const app = createApp(App)
// 注册插件
app.use(isAuthDirective)

app.mount('#app')

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3 的按钮权限控制可以通过如下步骤实现: 1. 定义权限列表:在后端定义权限列表,包括所有需要控制的操作,如增删改查等。 2. 获取用户权限:在用户登录成功后,从后端获取用户的权限列表。 3. 权限校验:在前端页面中需要控制权限按钮或链接中,根据用户权限列表判断是否显示或禁用该按钮或链接。 在实际开发中,可以将权限校验封装成一个指令或组件,方便重复使用。 以下是一个权限按钮的封装示例: ```vue <template> <button :disabled="!hasPermission" @click="handleClick"> <slot></slot> </button> </template> <script> import { computed } from 'vue' export default { props: { permission: { type: String, required: true } }, setup(props, { emit }) { const userPermissions = ['add', 'update', 'delete'] // 假设用户权限列表为 ['add', 'update', 'delete'] const hasPermission = computed(() => userPermissions.includes(props.permission)) function handleClick() { if (hasPermission.value) { emit('click') } else { // 如果没有权限,可以弹出提示 alert('您没有该操作的权限') } } return { hasPermission, handleClick } } } </script> ``` 在使用时,可以这样写: ```vue <template> <div> <PermissionButton permission="add" @click="handleAdd">添加</PermissionButton> <PermissionButton permission="update" @click="handleUpdate">修改</PermissionButton> <PermissionButton permission="delete" @click="handleDelete">删除</PermissionButton> </div> </template> <script> import PermissionButton from '@/components/PermissionButton.vue' export default { components: { PermissionButton }, methods: { handleAdd() { // 执行添加操作 }, handleUpdate() { // 执行修改操作 }, handleDelete() { // 执行删除操作 } } } </script> ``` 这样就可以根据用户权限列表控制按钮的显示和禁用了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值