mirror of
https://github.com/vbenjs/vue-vben-admin.git
synced 2025-08-27 23:50:23 +08:00
perf: remove optional chain
This commit is contained in:
2
src/components/Authority/index.ts
Normal file
2
src/components/Authority/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
import Authority from './src/index.vue';
|
||||
export default Authority;
|
@@ -1,61 +0,0 @@
|
||||
import { defineComponent, PropType, computed, unref } from 'vue';
|
||||
|
||||
import { PermissionModeEnum } from '/@/enums/appEnum';
|
||||
import { RoleEnum } from '/@/enums/roleEnum';
|
||||
import { usePermission } from '/@/hooks/web/usePermission';
|
||||
import { appStore } from '/@/store/modules/app';
|
||||
import { getSlot } from '/@/utils/helper/tsxHelper';
|
||||
|
||||
export default defineComponent({
|
||||
name: 'Authority',
|
||||
props: {
|
||||
// 指定角色可见
|
||||
value: {
|
||||
type: [Number, Array, String] as PropType<RoleEnum | RoleEnum[]>,
|
||||
default: '',
|
||||
},
|
||||
},
|
||||
setup(props, { slots }) {
|
||||
const getModeRef = computed(() => {
|
||||
return appStore.getProjectConfig.permissionMode;
|
||||
});
|
||||
|
||||
/**
|
||||
* 渲染角色按钮
|
||||
*/
|
||||
function renderRoleAuth() {
|
||||
const { value } = props;
|
||||
if (!value) {
|
||||
return getSlot(slots);
|
||||
}
|
||||
const { hasPermission } = usePermission();
|
||||
return hasPermission(value) ? getSlot(slots) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染编码按钮
|
||||
* 这里只判断是否包含,具体实现可以根据项目自行写逻辑
|
||||
*/
|
||||
function renderCodeAuth() {
|
||||
const { value } = props;
|
||||
if (!value) {
|
||||
return getSlot(slots);
|
||||
}
|
||||
const { hasPermission } = usePermission();
|
||||
return hasPermission(value) ? getSlot(slots) : null;
|
||||
}
|
||||
|
||||
return () => {
|
||||
const mode = unref(getModeRef);
|
||||
// 基于角色渲染
|
||||
if (mode === PermissionModeEnum.ROLE) {
|
||||
return renderRoleAuth();
|
||||
}
|
||||
// 基于后台编码渲染
|
||||
if (mode === PermissionModeEnum.BACK) {
|
||||
return renderCodeAuth();
|
||||
}
|
||||
return getSlot(slots);
|
||||
};
|
||||
},
|
||||
});
|
65
src/components/Authority/src/index.vue
Normal file
65
src/components/Authority/src/index.vue
Normal file
@@ -0,0 +1,65 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent, PropType, computed, unref } from 'vue';
|
||||
|
||||
import { PermissionModeEnum } from '/@/enums/appEnum';
|
||||
import { RoleEnum } from '/@/enums/roleEnum';
|
||||
import { usePermission } from '/@/hooks/web/usePermission';
|
||||
import { appStore } from '/@/store/modules/app';
|
||||
import { getSlot } from '/@/utils/helper/tsxHelper';
|
||||
|
||||
export default defineComponent({
|
||||
name: 'Authority',
|
||||
props: {
|
||||
// 指定角色可见
|
||||
value: {
|
||||
type: [Number, Array, String] as PropType<RoleEnum | RoleEnum[]>,
|
||||
default: '',
|
||||
},
|
||||
},
|
||||
setup(props, { slots }) {
|
||||
const getModeRef = computed(() => {
|
||||
return appStore.getProjectConfig.permissionMode;
|
||||
});
|
||||
|
||||
/**
|
||||
* 渲染角色按钮
|
||||
*/
|
||||
function renderRoleAuth() {
|
||||
const { value } = props;
|
||||
if (!value) {
|
||||
return getSlot(slots);
|
||||
}
|
||||
const { hasPermission } = usePermission();
|
||||
return hasPermission(value) ? getSlot(slots) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染编码按钮
|
||||
* 这里只判断是否包含,具体实现可以根据项目自行写逻辑
|
||||
*/
|
||||
function renderCodeAuth() {
|
||||
const { value } = props;
|
||||
if (!value) {
|
||||
return getSlot(slots);
|
||||
}
|
||||
const { hasPermission } = usePermission();
|
||||
return hasPermission(value) ? getSlot(slots) : null;
|
||||
}
|
||||
|
||||
return () => {
|
||||
const mode = unref(getModeRef);
|
||||
// 基于角色渲染
|
||||
if (mode === PermissionModeEnum.ROLE) {
|
||||
return renderRoleAuth();
|
||||
}
|
||||
|
||||
// 基于后台编码渲染
|
||||
if (mode === PermissionModeEnum.BACK) {
|
||||
return renderCodeAuth();
|
||||
}
|
||||
|
||||
return getSlot(slots);
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
Reference in New Issue
Block a user