Resources

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. 1

    Type-Safe defineProps

    beginnerhigh

    Utilize 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. 2

    Reactive Destructuring with toRefs

    intermediatestandard

    When 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. 3

    Template Ref Typing

    beginnerstandard

    Define 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. 4

    ShallowRef for Large Datasets

    advancedhigh

    Use 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. 5

    Custom Composable Extraction

    intermediatehigh

    Encapsulate reusable logic into functions prefixed with 'use'. Ensure they return an object of refs to maintain reactivity when consumed by components.

  6. 6

    Readonly State Protection

    intermediatestandard

    Wrap reactive state in readonly() before exporting it from a composable to prevent consumers from mutating state directly, enforcing a unidirectional data flow.

  7. 7

    Provide/Inject with InjectionKey

    advancedstandard

    Use InjectionKey symbols to provide type safety for dependency injection across deeply nested components, preventing naming collisions and runtime errors.

  8. 8

    WatchEffect for Automatic Dependencies

    intermediatestandard

    Use watchEffect() when you need to track multiple reactive dependencies automatically without explicitly listing them, such as when syncing local state to localStorage.

  9. 9

    Async Setup Handling

    advancedmedium

    Leverage Top-Level Await in <script setup> when working with asynchronous data fetching. Ensure the parent component is wrapped in a <Suspense> boundary.

  10. 10

    Computed Getters and Setters

    intermediatestandard

    Implement 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. 1

    Setup Store Syntax

    beginnerhigh

    Adopt 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. 2

    StoreToRefs for Destructuring

    beginnerstandard

    Apply 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. 3

    Cross-Store Interaction

    intermediatehigh

    Import and instantiate one store inside another to handle cross-domain logic. Pinia handles the dependency tree automatically without circular dependency issues.

  4. 4

    Store Action Subscriptions

    advancedmedium

    Use store.$onAction() to implement global logging, analytics, or undo/redo functionality by intercepting store calls before or after they execute.

  5. 5

    State Resetting in Setup Stores

    intermediatestandard

    Since 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. 6

    Pinia Plugin Persistence

    beginnerhigh

    Integrate 'pinia-plugin-persistedstate' to automatically sync store state with localStorage or sessionStorage using a simple 'persist: true' configuration key.

  7. 7

    Atomic State Updates with $patch

    intermediatestandard

    Use the $patch method to apply multiple changes to the state simultaneously, which can be more performant and easier to debug via Vue DevTools.

  8. 8

    Testing Stores with createTestingPinia

    intermediatehigh

    Use @pinia/testing to create a fresh store instance for every unit test, allowing you to mock actions and verify state changes in isolation.

  9. 9

    Composition-based Getters

    beginnerstandard

    Use computed() inside Setup Stores to create getters. This allows for complex derived state that caches results based on reactive dependencies.

  10. 10

    Hydrating SSR State

    advancedmedium

    Ensure 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. 1

    Vitest for SFC Unit Testing

    intermediatehigh

    Migrate 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. 2

    Vue Test Utils 'findComponent'

    beginnerstandard

    Use findComponent() to locate child components in the wrapper instead of querying DOM selectors, making tests more resilient to internal markup changes.

  3. 3

    Testing Composables in Isolation

    intermediatemedium

    Test composables without a component by wrapping them in a dummy component or using a simple wrapper function to provide the necessary lifecycle context.

  4. 4

    Volar Extension Setup

    beginnerhigh

    Disable 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. 5

    Component Code Splitting

    intermediatehigh

    Wrap heavy components (like charts or editors) in defineAsyncComponent() to split them into separate chunks, reducing the initial bundle size.

  6. 6

    VueUse useIntersectionObserver

    intermediatehigh

    Implement lazy loading of components or infinite scrolling using the useIntersectionObserver composable from the VueUse library for a performance boost.

  7. 7

    Teleport for Global UI

    beginnerstandard

    Use <Teleport to="body"> for modals and notifications to avoid CSS overflow or z-index issues caused by parent container constraints.

  8. 8

    Optimizing v-for with Keys

    beginnerstandard

    Always 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. 9

    Performance Profiling with DevTools

    intermediatestandard

    Use the Timeline tab in Vue DevTools to identify slow component renders and excessive reactivity triggers in complex views.

  10. 10

    Unplugin-Vue-Components Integration

    intermediatemedium

    Configure unplugin-vue-components in Vite to automatically import UI library components (like PrimeVue or Element Plus) as they are used in templates.