vue3 前端权限控制(按钮) 规范版

修改

后台使用的是若依系统,最近需要通过后台控制菜单、按钮权限;浅浅记录一下。

  1. 首utils先在后台系统设置好按钮权限字符等信息
  1. 创建文件夹store->permission.ts

持久化存储后端返回的权限信息,这里只关注permissions

import { defineStore } from 'pinia'
export const permissionStore = defineStore('permission', {
  state: (): {
    userState: {
      roles: []
      permissions: []
      tokenKey: ''
      user: {}
    }
  } => {
    return {
      userState: {
        roles: [],
        permissions: [],
        tokenKey: '',
        user: {}
      }
    }
  },
  actions: {
    setInfo(info: { permissions: []; roles: []; tokenKey: ''; user: {} }) {
      this.userState.permissions = info.permissions
      this.userState.roles = info.roles
      this.userState.tokenKey = info.tokenKey
      this.userState.user = info.user
    },
    getPermissions() {
      return this.userState.permissions
    }
  },
  persist: true
})
  1. 在登录之后,获取登录信息,包括角色,权限字符等
        //获取信息
        getInfo().then((res) => {
          const _permissionStore = permissionStore()
          _permissionStore.setInfo({
            permissions: res.permissions,
            roles: res.roles,
            tokenKey: res.tokenKey,
            user: res.user
          })
        })
4、utils文件夹中建立directive文件夹-->index.ts
import { permissionStore } from '@/store/permission'

import { debounce as _debounce } from 'lodash-es'
import { App, Directive } from 'vue'

const permission: Directive = {
  mounted: (el, binding, vnode) => {
    const store = permissionStore()
    const { value } = binding
    const all_permission = '*:*:*'
    const permissions = store.getPermissions()
    if (value && value instanceof Array && value.length > 0) {
      const permissionFlag = value
      const hasPermissions = permissions.some((permission) => {
        return all_permission === permission || permissionFlag.includes(permission)
      })
      if (!hasPermissions) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else {
      throw new Error(`请设置操作权限标签值`)
    }
  }
}
const install = function (Vue: App<Element>) {

  Vue.directive('permission', permission)
}
export default install
5、main.ts注册使用
import directive from './utils/directive'
app.use(directive)
6、组件中使用

v-permission="['system:user:add']"

其中system:user:add是菜单管理中设置的权限字符

            <el-col :span="1.5">
              <el-button
                class="add_btn"
                type="primary"
                v-permission="['system:user:add']"
                @click="createFrom"
                >新增</el-button
              >
            </el-col>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值