@vue/composition-api

repository·main·Indexed 26 days ago

https://github.com/vuejs/composition-api

A plugin that brings the Vue 3 Composition API to Vue 2.6 and earlier versions, allowing developers to use modern reactive patterns and composition logic in older Vue environments. It provides the reactivity API (reactive, ref, readonly), lifecycle hooks, effect scope management, and type-safe component definitions via defineComponent.

Tokens
3.2K
Snippets
0
Records
43
Agent score
84%

What's inside @vue/composition-api

  1. Install @vue/composition-api as a Vue plugin

    main

    To use the Composition API in a Vue 2 project, you must install it as a plugin using Vue.use(). This enables the setup() function and integrates the reactivity system with Vue 2's component lifecycle.

    Note: This plugin is specifically designed for Vue 2. It will trigger a warning if used with a version of Vue other than 2.x.

  2. Define type-safe components with defineComponent

    main

    Use defineComponent to define Vue components with full TypeScript type inference. It supports three main declaration formats:

    1. Object format with no props: Standard component options.
    2. Object format with array props: Pass an array of prop names; props are inferred as optional any types (useful for Vetur and TSX support).
    3. Object format with object props: Define props using an object declaration for detailed type control (similar to ExtractPropTypes).

    The function returns a VueProxy which provides type safety for data, computed properties, methods, and props.

  3. Use Ref API functions

    main

    The Ref API provides functions to create reactive references (wrappers for single values) and utilities to manage them.

    • ref(value): Creates a reactive ref object.
    • customRef(factory): Creates a ref with a custom implementation of getter/setter.
    • isRef(value): Returns true if the value is a ref.
    • createRef(): Creates a non-reactive ref (useful for template refs).
    • toRefs(reactiveObject): Converts a reactive object into an object of refs, where each property is a ref pointing to the corresponding property of the original object.
    • toRef(object, key): Creates a ref from a single property of a reactive object.
    • unref(value): Returns the value of a ref if it is a ref, otherwise returns the value itself.
    • shallowRef(value): Creates a ref that only tracks the .value property itself, not the contents of the value.
    • triggerRef(ref): Manually triggers updates for a shallowRef.
    • proxyRefs(object): Returns a proxy of an object where all properties are refs.
  4. Use the useCssModule hook to access CSS modules

    main

    The useCssModule hook allows you to access the CSS modules object (typically $style) within a component's setup() function.

    By default, it looks for a property named $style on the component instance. You can provide a custom name if your CSS module property is named differently.

    Requirements:

    • Must be called inside the setup() function of a component.
    • The component instance must have the specified CSS module property available.
  5. Convert reactive objects to refs with toRefs() and toRef()

    main
    Use toRefs() to convert a reactive object into a plain object where each property is a Ref. This is commonly used when destructuring reactive state while maintaining reactivity. Use toRef() to convert a single specific property of an object into a Ref.