# Composables API Reference Detailed usage examples for composables using ` ``` #### `useState(moduleName, keys)` Map state properties from a module to reactive computed refs. *Use when*: You need reactive access to store state. ```vue ``` #### `useGetters(moduleName, keys)` Map getters from a module to reactive computed refs. *Use when*: You need reactive access to store getters. ```vue ``` #### `useActions(moduleName, keys)` Map actions from a module to functions. *Use when*: You need to dispatch store actions. ```vue ``` #### `useMutations(moduleName, keys)` **Avoid unless it's absolutely necessary** Map mutations from a module to functions. Remember, Pinia has no mutations. Create an action in your store that calls the mutation, then use `useActions` to access it.This helper remains in case the mutation needs to be accessed directly for some reason. ```vue ``` ### Complete Example: Options API vs ` ``` **` ``` --- ## useGlobals Access global application properties. ### Available Functions - `useAppConfig()` - Application configuration - `useConstants()` - Global constants - `useFilters()` - Formatting functions - `useValidationErrors()` - Validation helpers ### Example: useAppConfig() ```vue ``` ### Example: useFilters() ```vue ``` --- ## Creating New Composables ### When to Create a Composable **Create composables for:** - Feature-specific logic that doesn't need global state (phonebook, NCOS, registrations) - Reusable business logic across components - Local state management with associated operations - API operations that don't affect global app state ### Template for Feature Composables Create composables that manage their own local state and business logic: ```javascript import { ref, computed } from 'vue' import { getNcosLevels, getNcosSet, setPreference } from 'src/api/subscriber' import { getSubscriberId } from 'src/auth' /** * Composable for managing NCOS (Network Class of Service) levels and sets. * Manages local state and provides API operations. */ export function useNcos() { // Local reactive state const levels = ref([]) const sets = ref([]) const loading = ref(false) const error = ref(null) // Computed properties const hasLevels = computed(() => levels.value.length > 0) const hasSets = computed(() => sets.value.length > 0) // Business logic functions const loadLevels = async () => { loading.value = true error.value = null try { const list = await getNcosLevels() levels.value = list.items.map(ncos => ({ label: ncos.level, value: ncos.id })) } catch (err) { error.value = err.message throw err } finally { loading.value = false } } const loadSets = async () => { loading.value = true error.value = null try { const list = await getNcosSet() sets.value = list.map(setNcos => ({ label: setNcos.name, value: setNcos.id })) } catch (err) { error.value = err.message throw err } finally { loading.value = false } } const updateLevel = async (value) => { loading.value = true error.value = null try { await setPreference(getSubscriberId(), 'ncos', value) } catch (err) { error.value = err.message throw err } finally { loading.value = false } } const reset = () => { levels.value = [] sets.value = [] error.value = null } return { // State levels, sets, loading, error, // Computed hasLevels, hasSets, // Actions loadLevels, loadSets, updateLevel, reset } } ``` ### Using the Feature Composable ```vue ``` ### Accessing Store Within a Composable If your composable needs global store state, use `useGetters` or `useActions`: ```javascript import { ref } from 'vue' import { useGetters } from 'src/composables/useStore' import { getCustomerPhonebook } from 'src/api/subscriber' export function useCustomerPhonebook() { // Access global state const { getCustomerId } = useGetters('user', ['getCustomerId']) // Local state const phonebook = ref([]) const loading = ref(false) const loadPhonebook = async () => { loading.value = true try { const list = await getCustomerPhonebook({ customerId: getCustomerId.value }) phonebook.value = list.data } finally { loading.value = false } } return { phonebook, loading, loadPhonebook } } ```