search
HomeWeb Front-endVue.jsDetailed explanation of how to use the v-model directive in the Vue documentation

Vue is a popular front-end framework. One of its core features is the responsive update of data, making it easier for developers to manage and maintain the user interface. As an important feature of Vue, the v-model directive can more conveniently realize two-way binding of data, making the user interface more flexible and easier to use. Let’s take a closer look at how to use the v-model directive in the Vue documentation.

1. The concept of v-model

The v-model instruction can realize two-way binding of data, that is, synchronize the data input by the user to the model, and also synchronize the data in the model to In the view, it is one of the important features in the Vue framework. The implementation is as follows:

1. Bind to the input box element

The v-model directive is built on the Vue form input element, including input boxes (text, password, number, etc.) , radio button (radio), check box (checkbox) and drop-down box (select), etc. The usage is as follows:

<input type="text" v-model="message">

The above code binds the message variable to the user input box by binding the v-model instruction to achieve two-way binding.

2. Bind to custom components

In addition to native form input elements, Vue also provides custom components that support the v-model directive. These components use model options to map internal values ​​to props, and only change this internal value when the input event fires. The sample code is as follows:

Vue.component('my-component', {
  props: ['value'],
  template: `
    <input
      :value="value"
      @input="$emit('input', $event.target.value)"
    >
  `
})

When defining a component, you need to declare a props option so that the v-model directive can bind the value variable to the component. Trigger the input event in the $emit function of the component and pass the value entered by the user to the component instance to achieve two-way binding.

2. Usage of v-model

1. Text input

The v-model command can be bound to the text input box element to achieve synchronous updates. In addition to the common single-line text boxes, Vue also supports the binding of multi-line text input boxes textarea, as shown below:

<input type="text" v-model="message"> 
 

For radio buttons and check boxes, v-model binds the selected state ( true or false), as shown below:

<input type="checkbox" v-model="checked">

2. Radio button

Using the v-model directive to bind the radio button box requires corresponding values ​​to the options, as shown below:

<input type="radio" v-model="picked" value="a">
<input type="radio" v-model="picked" value="b">

The v-model in the above code is bound to the picked variable. The value of each radio button corresponds to the value of the option. The value of the picked variable will also be updated when different options are selected.

3. Check box

Using the v-model instruction to bind the check box needs to be bound to a Boolean type variable, as shown below:

<input type="checkbox" id="checkbox" v-model="checked">
<label for="checkbox">{{ checked }}</label>

Complex The selected state of the check box is bound to a Boolean type variable, and {{ checked }} is bound to the value of the variable.

4. Drop-down box

The drop-down box also supports binding through the v-model instruction, as shown below:

<select v-model="selected">
  <option value="a">A</option>
  <option value="b">B</option>
  <option value="c">C</option>
</select>

The v-model binding in the above code is The selected variable, each option in the drop-down box corresponds to the value.

3. Modifiers of v-model

Modifiers are extension symbols that are used after the v-model directive to change the default behavior of v-model. Vue provides multiple modifiers, which are explained one by one below:

1.lazy

By default, the v-model directive is a two-way binding triggered by the input event, that is, every Each input will update the data to the model, and the lazy modifier will change this behavior to a change event, which will allow v-model to synchronize the data to the model only in the blur event. The sample code is as follows:

<input type="text" v-model.lazy="message">

2.number

For the v-model directive with the "number" modifier, Vue will automatically convert the input into the Number type. The sample code is as follows:

<input type="text" v-model.number="message">

3.trim

The v-model command with the "trim" modifier will automatically filter the first and last blank characters entered by the user. The sample code is as follows:

<input type="text" v-model.trim="message">

4. The principle of v-model

The implementation principle of the v-model instruction is to use data hijacking technology. When the value attribute of the user input box changes, v-model will monitor the change and synchronize the change to the model. At the same time, it will also Changes are synchronized to the view. The specific implementation method is as follows:

1. Hijack data

The Vue framework hijacks data through the Object.defineProperty method. When the model variable is read, the get method will be triggered; and when When a model variable is assigned a value, the set method will be triggered; in the set method, the value of the model is updated and a notification of data update is triggered.

2. Monitor the DOM of the user input box

When the value of the user input box changes, the input event will be triggered; after receiving the input event, the v-model instruction will Values ​​entered by the user are synchronized into the model.

3. Notify the model

When the value of the model changes, the v-model directive will pass the new value to the form element bound to the directive to update the user interface.

5. Summary

The v-model directive is one of the important features in the Vue framework and plays a core role in the two-way binding of form data. Through the use of v-model, development efficiency and code maintainability can be improved in Vue development. At the same time, mastering the modifiers and principles of v-model will help you better understand the operating mechanism of the Vue framework and improve your front-end development skills.

The above is the detailed content of Detailed explanation of how to use the v-model directive in the Vue documentation. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Frontend Landscape: How Netflix Approached its ChoicesThe Frontend Landscape: How Netflix Approached its ChoicesApr 15, 2025 am 12:13 AM

Netflix's choice in front-end technology mainly focuses on three aspects: performance optimization, scalability and user experience. 1. Performance optimization: Netflix chose React as the main framework and developed tools such as SpeedCurve and Boomerang to monitor and optimize the user experience. 2. Scalability: They adopt a micro front-end architecture, splitting applications into independent modules, improving development efficiency and system scalability. 3. User experience: Netflix uses the Material-UI component library to continuously optimize the interface through A/B testing and user feedback to ensure consistency and aesthetics.

React vs. Vue: Which Framework Does Netflix Use?React vs. Vue: Which Framework Does Netflix Use?Apr 14, 2025 am 12:19 AM

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

The Choice of Frameworks: What Drives Netflix's Decisions?The Choice of Frameworks: What Drives Netflix's Decisions?Apr 13, 2025 am 12:05 AM

Netflix mainly considers performance, scalability, development efficiency, ecosystem, technical debt and maintenance costs in framework selection. 1. Performance and scalability: Java and SpringBoot are selected to efficiently process massive data and high concurrent requests. 2. Development efficiency and ecosystem: Use React to improve front-end development efficiency and utilize its rich ecosystem. 3. Technical debt and maintenance costs: Choose Node.js to build microservices to reduce maintenance costs and technical debt.

React, Vue, and the Future of Netflix's FrontendReact, Vue, and the Future of Netflix's FrontendApr 12, 2025 am 12:12 AM

Netflix mainly uses React as the front-end framework, supplemented by Vue for specific functions. 1) React's componentization and virtual DOM improve the performance and development efficiency of Netflix applications. 2) Vue is used in Netflix's internal tools and small projects, and its flexibility and ease of use are key.

Vue.js in the Frontend: Real-World Applications and ExamplesVue.js in the Frontend: Real-World Applications and ExamplesApr 11, 2025 am 12:12 AM

Vue.js is a progressive JavaScript framework suitable for building complex user interfaces. 1) Its core concepts include responsive data, componentization and virtual DOM. 2) In practical applications, it can be demonstrated by building Todo applications and integrating VueRouter. 3) When debugging, it is recommended to use VueDevtools and console.log. 4) Performance optimization can be achieved through v-if/v-show, list rendering optimization, asynchronous loading of components, etc.

Vue.js and React: Understanding the Key DifferencesVue.js and React: Understanding the Key DifferencesApr 10, 2025 am 09:26 AM

Vue.js is suitable for small to medium-sized projects, while React is more suitable for large and complex applications. 1. Vue.js' responsive system automatically updates the DOM through dependency tracking, making it easy to manage data changes. 2.React adopts a one-way data flow, and data flows from the parent component to the child component, providing a clear data flow and an easy-to-debug structure.

Vue.js vs. React: Project-Specific ConsiderationsVue.js vs. React: Project-Specific ConsiderationsApr 09, 2025 am 12:01 AM

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

How to jump a tag to vueHow to jump a tag to vueApr 08, 2025 am 09:24 AM

The methods to implement the jump of a tag in Vue include: using the a tag in the HTML template to specify the href attribute. Use the router-link component of Vue routing. Use this.$router.push() method in JavaScript. Parameters can be passed through the query parameter and routes are configured in the router options for dynamic jumps.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft