forked from mengyxu/noob-components
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1.9 KiB
1.9 KiB
useActionPers
Action permissions management with Vuex store integration.
Purpose
Retrieves and manages action permissions from Vuex store, typically for controlling UI element visibility based on user permissions.
Signature
export function useActionPers(parent?: string): {
get: (per: string) => boolean;
update: () => Promise<void>;
actionPers: Ref<Record<string, boolean>>;
}
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
parent |
string |
route.path |
Permission namespace/path |
Return Value
| Property | Type | Description |
|---|---|---|
get |
(per: string) => boolean |
Check if specific permission is granted |
update |
() => Promise<void> |
Force refresh permissions from store |
actionPers |
Ref<Record<string, boolean>> |
Reactive permission map |
Usage Pattern
// Example: plugs/composables/useActionPers.ts
import { useActionPers } from "plugs/composables";
const { get, update, actionPers } = useActionPers();
// Check permission
if (get("user:create")) {
showCreateButton();
}
// Use in template
// v-if="actionPers.value.user:delete"
// Refresh permissions
await update();
Vuex Store Structure
Expects store to have:
state.actionPers[parent]- Array of permission codesdispatch("updateActionPers", parent)- Action to refresh permissions
Key Implementation Details
- Permission mapping: Converts permission array to object with all values set to
true - Auto-fetch on mount: Calls
update()inonMountedhook - Route-based namespace: Uses current route path as default namespace
Dependencies
vue-onMounted,toRefvue-router-useRoutevuex-useStore
Anti-patterns
- Do not hardcode permission checks; always go through
get()method - Do not call
update()too frequently; permissions are typically stable