index.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <template>
  2. <div class="h-full">
  3. <n-card title="权限切换" class="h-full shadow-sm rounded-16px">
  4. <div class="pb-12px">
  5. <n-gradient-text type="primary" :size="20">当前用户的权限:{{ auth.userInfo.userRole }}</n-gradient-text>
  6. </div>
  7. <n-select :value="auth.userInfo.userRole" class="w-120px" size="small" />
  8. <div class="py-12px">
  9. <n-gradient-text type="primary" :size="20">权限指令 v-permission</n-gradient-text>
  10. </div>
  11. <div>
  12. <n-button v-permission="'super'" class="mr-12px">super可见</n-button>
  13. <n-button v-permission="'admin'" class="mr-12px">admin可见</n-button>
  14. <n-button v-permission="['admin', 'user']">admin和test可见</n-button>
  15. </div>
  16. <div class="py-12px">
  17. <n-gradient-text type="primary" :size="20">权限函数 hasPermission</n-gradient-text>
  18. </div>
  19. <n-space>
  20. <n-button v-if="hasPermission('member')">super可见</n-button>
  21. <n-button v-if="hasPermission('admin')">admin可见</n-button>
  22. <n-button v-if="hasPermission(['admin', 'user'])">admin和user可见</n-button>
  23. </n-space>
  24. </n-card>
  25. </div>
  26. </template>
  27. <script setup lang="ts">
  28. import { watch } from 'vue';
  29. // import type { SelectOption } from 'naive-ui';
  30. // import { userRoleOptions } from '@/constants';
  31. import { useAppStore, useAuthStore } from '@/store';
  32. import { usePermission } from '@/composables';
  33. const app = useAppStore();
  34. const auth = useAuthStore();
  35. const { hasPermission } = usePermission();
  36. // const options: SelectOption[] = userRoleOptions;
  37. watch(
  38. () => auth.userInfo.userRole,
  39. async () => {
  40. app.reloadPage();
  41. }
  42. );
  43. </script>
  44. <style scoped></style>