Preface
In Vue 3.4,v-model
The two-way binding mechanism has been further optimized. With the introduction of Vue 3.3defineModel
Macros, developers can implement bidirectional data binding within components more concisely. This article will explain in detailv-model
Basic usage, custom attribute names, multiple model bindings, anddefineModel
practical application.
Basic usage: v-model and defineModel
In Vue 3,v-model
The component will be bound by defaultmodelValue
attributes and passupdate:modelValue
Events implement two-way data transfer between parent and child components.
Traditional writing using v-model
Parent component
<template> <ChildComponent v-model="value" /> </template> <script setup> import { ref } from 'vue'; import ChildComponent from './'; const value = ref('Hello Vue 3'); </script>
Subcomponents (traditional writing)
<template> <input type="text" :value="modelValue" @input="$emit('update:modelValue', $)" /> </template> <script setup> defineProps(['modelValue']); const emit = defineEmits(['update:modelValue']); </script>
Simplified writing using defineModel
Starting with Vue 3.3, subcomponents can be useddefineModel
The macro simplifies the above code.
Subcomponents
<template> <input type="text" v-model="model" /> </template> <script setup> const model = defineModel(); </script>
illustrate
-
defineModel
: No manual definition requiredprops
andemits
, automatic processingmodelValue
andupdate:modelValue
。 - Parent component's
v-model
Will be automatically bound tomodel
variable.
Custom v-model property name
Except for the defaultmodelValue
, Vue 3 supports customizationv-model
attribute name. existdefineModel
You can also pass parameters in the property name of the bound.
Example
Parent component
<template> <ChildComponent v-model:title="titleValue" /> </template> <script setup> import { ref } from 'vue'; import ChildComponent from './'; const titleValue = ref('Default Title'); </script>
Subcomponents
<template> <input type="text" v-model="title" /> </template> <script setup> const title = defineModel('title'); </script>
illustrate
-
Custom attribute name:pass
defineModel('title')
,Willv-model:title
Bind totitle
variable. - In parent component
v-model:title
It will be mapped automatically astitle
variable.
Equivalent to:
<ChildComponent :title="titleValue" @update:title="titleValue = $event" />
Multiple v-model bindings
In the same component, multiple can be boundv-model
property,defineModel
Supports multiple bindings to handle different data models separately.
Example
Parent component
<template> <ChildComponent v-model:title="titleValue" v-model:content="contentValue" /> </template> <script setup> import { ref } from 'vue'; import ChildComponent from './'; const titleValue = ref('My Title'); const contentValue = ref('This is content'); </script>
Subcomponents
<template> <div> <input type="text" v-model="title" placeholder="Title" /> <textarea v-model="content" placeholder="Content"></textarea> </div> </template> <script setup> const title = defineModel('title'); const content = defineModel('content'); </script>
illustrate
- Each
defineModel
All can define an independent binding property. - Parent component passes
v-model:title
andv-model:content
Bind different attributes respectively.
Combining v-model with extra properties
In the component,v-model
Can be used simultaneously with other attributes. For example, you can add extra props such as placeholders to the input box.
Example
Parent component
<template> <ChildComponent v-model="value" placeholder="Please enter" /> </template> <script setup> import { ref } from 'vue'; import ChildComponent from './'; const value = ref(''); </script>
Subcomponents
<template> <input type="text" v-model="model" :placeholder="placeholder" /> </template> <script setup> const model = defineModel(); defineProps({ placeholder: String, // Receive extra attributes}); </script>
illustrate
-
v-model
Boundedmodel
, realize two-way data binding. -
placeholder
It's ordinaryprops
, can provide additional features.
Summarize
New features of v-model
-
Default binding
modelValue
andupdate:modelValue
event, simplify data binding between parent and child components. -
Supports custom attribute names,like
v-model:title
, flexibly bind multiple data. -
Support multiple
v-model
, bind multiple properties in the same component. -
defineModel
MacroProvides a simpler writing method, automatically handling props and emits. - It can be used in combination with ordinary properties to enhance the scalability of components.
Benefits of using defineModel
- The code is more concise and reduces duplicate code in templates and scripts.
- Improve maintainability and avoid manual management
props
andemits
。 - Provides flexible two-way binding methods, supporting custom property names and multiple models.
By reasonable usedefineModel
andv-model
, Vue 3.4 provides a more modern and elegant two-way binding solution for components.
This is the article about how vue3 defineModel achieves two-way binding. For more related contents of vue3 defineModel, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!