console.log(🎉);|

yunChuans

园龄:3年4个月粉丝:6关注:0

2023-09-07 23:08阅读: 54评论: 0推荐: 0

vue组件级别的权限控制

核心思想:尽量减少对组件的侵入性,也就是权限控制的代码剔除出来

实现

authority.vue

<template>
  <div>
  // 将用户权限通过作用域插槽传递出去
    <slot v-if="showSlot" :permissions="rights"></slot>
  </div>
</template>

<script setup lang="ts">

const props = defineProps({
  permissions: {
    type: [String,Array],
    required: true
  },
})

const rights = ref(['user-control','system-control','goods-control'])

const showSlot = computed(()=>{
  // 用户无任何权限
  if(!rights){
    return false
  }
  // 没传入权限
  if(!props.permissions){
    return true
  }
  // 有传入
  if(Array.isArray(props.permissions)){
    return props.permissions.every((item:any)=>rights.value.includes(item))
  }else {
    return rights.value.includes(props.permissions)
  }
})

</script>

<style lang="scss" scoped>

</style>

index.vue

<template>
  <div>
    <authority :permissions="['user-control','system-control']" v-slot="{permissions}">
      <nut-button :disabled="!permissions.includes('system-control')" type="warning">sssss</nut-button>
    </authority>
    <ul>
      <li class="asd">45</li>
      <li>45</li>
      <li>45</li>
    </ul>
    <div class="aaa"></div>
  </div>
</template>

<script setup lang="ts" name="home">
// import { Add } from '@nutui/icons-vue'
import authority from '@/components/authority/authority.vue'

const test = ref('111')
console.log(test)

</script>

<style lang="scss" scoped>
.asd {
  font-size: p2r(32);
  color: green;
}
.aaa {
  width: p2r(200);
  height: p2r(100);
  background-color: pink;
}
</style>

本文作者:yunChuans

本文链接:https://www.cnblogs.com/zychuan/p/17686350.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   yunChuans  阅读(54)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 恋ダコ (茧•爱) 恋ダコ
  2. 2 君がいて水になる 正しい偽りからの起床
恋ダコ (茧•爱) - 恋ダコ
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.