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
refwraps a single value, whilereactive()creates a reactive proxy for an object. Arefcan hold any value type, including deeply nested objects, arrays, or built-ins likeMap.reactive()only works for object types such as objects, arrays,MapandSet, and it cannot hold primitives likestring,numberorboolean. Because of these limits, Vue recommendsref()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
- Access: A ref's value is read and written through
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
propsoption, or withdefinePropsinside<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 withv-forto 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
- Static values: Write the value directly as an attribute, such as
How do I use v-model on a component?
To use
v-modelon a component, the child declares a model withdefineModel(), which is the recommended approach from Vue 3.4 onward. The parent then binds a value withv-model. The ref returned bydefineModel()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 samev-modelusage.Under the hood,
defineModelis a convenience macro that declares amodelValueprop and emits anupdate:modelValueevent when the local value changes. Its options can be passed through to that prop, such asrequiredordefault. Adefaultcan 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 withdefinePropsanddefineEmits.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 callsdefineModel('title'). Prop options go after the model name. Pre-3.4 code instead declares atitleprop and emitsupdate:title.Custom modifiers are supported too. The parent can write
v-model.capitalize, and the child reads the modifiers from the second element of thedefineModel()return value. Asetoption can transform the value based on those modifiers. With an argument and modifiers together, the modifiers prop is named the argument plus "Modifiers", such astitleModifiers.Finally, you can bind several models on one component by giving each
v-modela different argument, for examplev-model:first-nameandv-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