Basic usage
In basic usage,v-model
Directives can be used on the following three types of form input elements:
<input>
<textarea>
<select>
Use on these elementsv-model
Directives that can bind its value to a data attribute in two-way. For example:
<div > <p>Message is: {{ message }}</p> <input v-model="message"> </div>
const app = ({ data() { return { message: 'Hello Vue!' }; } }); ('#app');
In this example, we usev-model
The command will<input>
The value of the element andmessage
The data attributes are bidirectionally bound. When the user enters text in the input box,message
The value of , will be updated in real time and vice versa.
Advanced Usage
In advanced usage,v-model
The instructions can be used in the following situations:
- Custom modifiers
- Custom events
- Use on components
v-model
existv-model
In directives, modifiers can be used to modify their default behavior. For example:
Custom modifiers
-
.lazy
: The data will not be updated when the input box loses focus or the user presses the Enter key. -
.number
: Automatically convert the value entered by the user to a numeric type. -
.trim
: Automatically remove the beginning and end spaces entered by the user.
For example:
<div > <p>Message is: {{ message }}</p> <input ="message"> <input ="age"> <input ="name"> </div>
const app = ({ data() { return { message: 'Hello Vue!', age: 0, name: '' }; } }); ('#app');
In this example, we use.lazy
、.number
and.trim
Three modifiers have been modified separatelyv-model
The default behavior of directives.
Custom events
In some cases, we need to trigger a custom event when the data is updated, which can be usedv-model
Custom event function for directives. For example:
<div > <p>Message is: {{ message }}</p> <input v-model="message" @input="onInput"> </div>
const app = ({ data() { return { message: 'Hello Vue!' }; }, methods: { onInput(event) { (); } } }); ('#app');
In this example, we<input>
Used on the elements at the same timev-model
Instructions and@input
Event listener. When the user enters text in the input box,message
The value of theonInput
method.
Use on componentsv-model
In custom components we can usev-model
Directive to implement bidirectional data binding with parent components. For example:
<div > <p>Message is: {{ message }}</p> <my-input v-model="message"></my-input> </div>
const MyInput = { props: ['modelValue'], template: ` <input :value="modelValue" @input="$emit('update:modelValue', $)"> ` }; const app = ({ components: { MyInput }, data() { return { message: 'Hello Vue!' }; } }); ('#app');
In this example, we define a name calledMyInput
Custom components and used in itv-model
instruction. In the component, we useprops
and$emit
To implement bidirectional data binding with the parent component. When the user enters text in the input box,message
The value of , will be updated in real time and vice versa.
Principle & Default
Used in componentsv-model
The principle is: the parent component passes a value to the props property of the child component, and the child component passes the$emit
The method triggers a custom event that passes the new value back to the parent component. After the parent component receives the new value, it updates the data, thereby achieving two-way binding.
Use in componentsv-model
When a component needs to be definedmodel
Option, which is used to specifyv-model
Binded props and events.model
An option is an object that contains the following two properties:
-
prop
: Used to specify the binding prop name, default isvalue
。 -
event
: Used to specify the event name that triggers the update, the default isinput
。
For example, in a custom input box component, we can usev-model
:
<template> <input :value="value" @input="$emit('input', $)"> </template> <script> export default { props: ['value'], model: { prop: 'value', event: 'input' } } </script>
In this example, we define avalue
prop, used to receive the value passed by the parent component. At the same time, we define amodel
Option, specifiedprop
forvalue
,event
forinput
. This means that when the user enters content in the input box, the component triggers ainput
event and pass new value$emit
The method is passed back to the parent component. After the parent component receives the new value, it updates the data, thereby achieving two-way binding.
When using this component in the parent component, you can use it like thisv-model
:
<template> <div> <p>Message is: {{ message }}</p> <my-input v-model="message"></my-input> </div> </template> <script> import MyInput from './'; export default { components: { MyInput }, data() { return { message: 'Hello Vue!' }; } } </script>
In this example, we use the parent componentv-model
Instructions,message
Data binding tomy-input
on component. When the user enters content in the input box, the component triggers ainput
event and pass the new value back to the parent component. After the parent component receives the new value, update itmessage
data, thus achieving two-way binding.
It should be noted that it is used in componentsv-model
must be defined in the componentmodel
Options, otherwisev-model
I don't know which prop and event to bind to. Also, use it in the parent componentv-model
When , the value of the child component must be bound to a data attribute, otherwise two-way binding cannot be achieved.
In short, use in componentsv-model
Two-way data binding between components and parent components can be implemented, and the principle is implemented through props and events. Use in componentsv-model
When, it needs to be definedmodel
Options, specify the bound prop and event. Use in parent componentv-model
When , the value of the child component needs to be bound to a data attribute to achieve two-way binding.
This is the article about the detailed explanation of v-mode in vue and specific usage examples. For more related content on vue v-mode usage, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!