100 Vue (general) resources for developers
This resource provides a technical roadmap for implementing Vue 3 features in enterprise environments, focusing on the Composition API, TypeScript integration, and Pinia state management. It prioritizes practical patterns over theory to accelerate development in high-scale codebases.
Composition API and TypeScript Patterns
- 1
Type-Safe defineProps
beginnerhighUtilize TypeScript interfaces within defineProps to enforce strict prop validation. Use the 'withDefaults' compiler macro to provide default values for optional props without losing type inference.
- 2
Reactive Destructuring with toRefs
intermediatestandardWhen destructuring props or reactive objects, wrap the source in toRefs() to prevent losing reactivity. This is critical when passing individual prop values to composables.
- 3
Template Ref Typing
beginnerstandardDefine template refs using the generic ref syntax: const myInput = ref<HTMLInputElement | null>(null). This ensures proper IDE autocompletion and type checking for DOM methods.
- 4
ShallowRef for Large Datasets
advancedhighUse shallowRef() instead of ref() for large arrays or complex objects that do not require deep reactivity. This reduces overhead by only tracking the top-level reference change.
- 5
Custom Composable Extraction
intermediatehighEncapsulate reusable logic into functions prefixed with 'use'. Ensure they return an object of refs to maintain reactivity when consumed by components.
- 6
Readonly State Protection
intermediatestandardWrap reactive state in readonly() before exporting it from a composable to prevent consumers from mutating state directly, enforcing a unidirectional data flow.
- 7
Provide/Inject with InjectionKey
advancedstandardUse InjectionKey symbols to provide type safety for dependency injection across deeply nested components, preventing naming collisions and runtime errors.
- 8
WatchEffect for Automatic Dependencies
intermediatestandardUse watchEffect() when you need to track multiple reactive dependencies automatically without explicitly listing them, such as when syncing local state to localStorage.
- 9
Async Setup Handling
advancedmediumLeverage Top-Level Await in <script setup> when working with asynchronous data fetching. Ensure the parent component is wrapped in a <Suspense> boundary.
- 10
Computed Getters and Setters
intermediatestandardImplement writable computed properties by providing an object with get() and set(value) methods, useful for wrapping v-model logic in child components.
State Management with Pinia
- 1
Setup Store Syntax
beginnerhighAdopt the function-based 'Setup Store' syntax in Pinia to leverage the same mental model as the Composition API, making it easier to share logic between stores and components.
- 2
StoreToRefs for Destructuring
beginnerstandardApply storeToRefs() when extracting state or getters from a Pinia store to keep them reactive. Note that actions should be destructured directly from the store object.
- 3
Cross-Store Interaction
intermediatehighImport and instantiate one store inside another to handle cross-domain logic. Pinia handles the dependency tree automatically without circular dependency issues.
- 4
Store Action Subscriptions
advancedmediumUse store.$onAction() to implement global logging, analytics, or undo/redo functionality by intercepting store calls before or after they execute.
- 5
State Resetting in Setup Stores
intermediatestandardSince Setup Stores don't have a built-in $reset, manually implement a reset function that returns the store state to its initial values to handle user logout scenarios.
- 6
Pinia Plugin Persistence
beginnerhighIntegrate 'pinia-plugin-persistedstate' to automatically sync store state with localStorage or sessionStorage using a simple 'persist: true' configuration key.
- 7
Atomic State Updates with $patch
intermediatestandardUse the $patch method to apply multiple changes to the state simultaneously, which can be more performant and easier to debug via Vue DevTools.
- 8
Testing Stores with createTestingPinia
intermediatehighUse @pinia/testing to create a fresh store instance for every unit test, allowing you to mock actions and verify state changes in isolation.
- 9
Composition-based Getters
beginnerstandardUse computed() inside Setup Stores to create getters. This allows for complex derived state that caches results based on reactive dependencies.
- 10
Hydrating SSR State
advancedmediumEnsure the initial state from the server is passed to the Pinia instance on the client side to prevent hydration mismatch errors in Nuxt or custom SSR setups.
Testing and Optimization
- 1
Vitest for SFC Unit Testing
intermediatehighMigrate from Jest to Vitest for faster unit testing of Single File Components (SFCs), taking advantage of Vite's native module resolution and fast HMR.
- 2
Vue Test Utils 'findComponent'
beginnerstandardUse findComponent() to locate child components in the wrapper instead of querying DOM selectors, making tests more resilient to internal markup changes.
- 3
Testing Composables in Isolation
intermediatemediumTest composables without a component by wrapping them in a dummy component or using a simple wrapper function to provide the necessary lifecycle context.
- 4
Volar Extension Setup
beginnerhighDisable the built-in VS Code 'TypeScript Language Features' for Vue projects and enable Volar's Take Over Mode to ensure accurate type checking in templates.
- 5
Component Code Splitting
intermediatehighWrap heavy components (like charts or editors) in defineAsyncComponent() to split them into separate chunks, reducing the initial bundle size.
- 6
VueUse useIntersectionObserver
intermediatehighImplement lazy loading of components or infinite scrolling using the useIntersectionObserver composable from the VueUse library for a performance boost.
- 7
Teleport for Global UI
beginnerstandardUse <Teleport to="body"> for modals and notifications to avoid CSS overflow or z-index issues caused by parent container constraints.
- 8
Optimizing v-for with Keys
beginnerstandardAlways use a unique identifier (like an ID) for :key in v-for loops. Avoid using array indices to prevent rendering glitches during list mutations.
- 9
Performance Profiling with DevTools
intermediatestandardUse the Timeline tab in Vue DevTools to identify slow component renders and excessive reactivity triggers in complex views.
- 10
Unplugin-Vue-Components Integration
intermediatemediumConfigure unplugin-vue-components in Vite to automatically import UI library components (like PrimeVue or Element Plus) as they are used in templates.