Prechádzať zdrojové kódy

feat: v-permission新增传入字符串、数组,同时兼容原来的传参方式

louhangfei 11 mesiacov pred
rodič
commit
8b1d2e3335
1 zmenil súbory, kde vykonal 16 pridanie a 2 odobranie
  1. 16 2
      src/directives/permission.ts

+ 16 - 2
src/directives/permission.ts

@@ -1,11 +1,25 @@
 import { ObjectDirective } from 'vue';
 import { usePermission } from '@/hooks/web/usePermission';
 
+/** 支持v-permission直接传入string, string[],以及兼容原来的方法 */
 export const permission: ObjectDirective = {
   mounted(el: HTMLButtonElement, binding) {
-    if (binding.value == undefined) return;
-    const { action, effect } = binding.value;
+    const val = binding.value;
+    if (val === undefined) return;
+
+    let permissionVal: string[] = [];
+    if (typeof val === 'string') {
+      permissionVal = [val];
+    } else if (val instanceof Array) {
+      permissionVal = val;
+    }
     const { hasPermission } = usePermission();
+    if (!hasPermission(permissionVal)) {
+      el.remove();
+      return;
+    }
+    const { action, effect } = val;
+
     if (!hasPermission(action)) {
       if (effect == 'disabled') {
         el.disabled = true;