EveryPage

Unofficial example

Ask the Vue docs

The Ask this site box, reading vuejs.org/guide. Type a question and it answers from those pages, with links.

Loading the box…

An unofficial example. Ask this site is not affiliated with or endorsed by the Vue project. The Vue docs belong to their authors and are read here under their CC BY 4.0 licence.

Questions it answered

Real answers from this box.

  • What is the difference between ref and reactive?

    A ref wraps a single value, while reactive() creates a reactive proxy for an object. A ref can hold any value type, including deeply nested objects, arrays, or built-ins like Map. reactive() only works for object types such as objects, arrays, Map and Set, and it cannot hold primitives like string, number or boolean. Because of these limits, Vue recommends ref() as the primary API for declaring reactive state.

    The two also differ in how you access them and how they behave when passed around:

    • Access: A ref's value is read and written through .value, which is how Vue detects access and mutation. A reactive object's properties are accessed directly, without .value.
    • Reassignment: A reactive object must keep the same reference, since you cannot easily replace it without losing the reactivity connection. A ref can be replaced by changing its .value.
    • Destructuring and function arguments: Destructuring a reactive object's primitive property into a variable, or passing that property to a function, loses the reactivity connection. A ref can be passed into functions while keeping access to the latest value and its reactivity connection.
    • Unwrapping: A ref nested as a property of a deep reactive object is unwrapped automatically. This does not happen for refs inside reactive arrays or native collections like Map, which need .value.

    Under the hood, Vue 3 uses Proxies for reactive objects and getters and setters for refs. A non-primitive value inside a ref is turned into a reactive proxy via reactive().

    Answered from Reactivity Fundamentals ​ — Deep Reactivity ​, Reactivity Fundamentals ​ — Limitations of reactive() ​, Reactivity Fundamentals ​ — Why Refs? ​, Reactivity Fundamentals ​ — As Reactive Object Property ​, Composables ​ — Accepting Reactive State ​, Reactivity Fundamentals ​ — Caveat in Arrays and Collections ​, Reactivity in Depth ​ — How Reactivity Works in Vue ​

  • How do I pass props to a child component?

    You declare the props in the child component, then pass values to them as custom attributes in the parent. In the child, props are declared with the props option, or with defineProps inside <script setup>, and declared props are automatically exposed to the template. A component can accept as many props as you need.

    In the parent, you pass values in one of two ways:

    • Static values: Write the value directly as an attribute, such as title="My journey with Vue".
    • Dynamic values: Use v-bind (the : shorthand), such as :title="post.title", which is useful when the content isn't known ahead of time. You can combine it with v-for to render one component per item.

    By default, any value can be passed to any prop. Props flow one way, from parent to child. When the parent updates, the child receives the new value, but the child should not mutate a prop, and Vue warns if it does. If a child needs the prop as a starting value, copy it into local data. If it needs a transformed version, use a computed property. Passing an object or array lets the child change its nested properties, but this is best avoided unless the two components are tightly coupled; usually the child should emit an event so the parent makes the change.

    Answered from Components Basics ​ — Passing Props ​, Props ​ — One-Way Data Flow ​, Props ​ — Mutating Object / Array Props ​

  • How do I use v-model on a component?

    To use v-model on a component, the child declares a model with defineModel(), which is the recommended approach from Vue 3.4 onward. The parent then binds a value with v-model. The ref returned by defineModel() stays in sync with the parent's bound value, and mutating it in the child updates the parent as well. It can also be bound to a native input inside the child, so the component keeps the same v-model usage.

    Under the hood, defineModel is a convenience macro that declares a modelValue prop and emits an update:modelValue event when the local value changes. Its options can be passed through to that prop, such as required or default. A default can cause parent and child to fall out of sync if the parent does not supply a value, so use it with care. Before 3.4, the same behavior was written with defineProps and defineEmits.

    You can also use a named argument, so the binding targets a different prop and event. The parent writes v-model:title, and the child calls defineModel('title'). Prop options go after the model name. Pre-3.4 code instead declares a title prop and emits update:title.

    Custom modifiers are supported too. The parent can write v-model.capitalize, and the child reads the modifiers from the second element of the defineModel() return value. A set option can transform the value based on those modifiers. With an argument and modifiers together, the modifiers prop is named the argument plus "Modifiers", such as titleModifiers.

    Finally, you can bind several models on one component by giving each v-model a different argument, for example v-model:first-name and v-model:last-name. Each one syncs to its own prop without extra options.

    Answered from Component v-model ​ — Basic Usage ​, Component v-model ​ — Under the Hood ​, Component v-model ​ — v-model Arguments ​, Component v-model ​ — Handling v-model Modifiers ​, Component v-model ​ — Modifiers for v-model with Arguments ​, Component v-model ​ — Multiple v-model Bindings ​

Add this to your own docs

Paste your docs address to try it, free and without signing up. Then it is one line of code.

DocusaurusGitBookMintlifyWordPressReadMeMkDocsVitePressStarlightSphinxHugoJekyllNext.jsGhostWebflow