Learn v-model and its modifiers in one article

Learn v-model and its modifiers in one article

Preface

Used to bind data and element value to achieve two-way binding.

When the value of the element changes, the data also changes, and conversely, when the data changes, the value of the element also changes.

In most cases, all user input values ​​must go through v-model, which is easy to use, safe, and powerful.

<div class="app">
	<input type="text" v-model="name">
	{{name}}
</div>
var app = new Vue({
	el:'.app',
	data:{
		name:'Alice'
	}
});

Modifiers of v-model:

lazy

Lazy means lazy update, which will not be updated immediately.

In fact, it triggers a change event.

Advantages: The form validation result will be displayed only when the user has finished inputting, whether it is correct or wrong. But don't disturb the user while he is typing. In addition, the performance has also been slightly improved (but it is very small and can be ignored)

<div class="app">
    <input type="text" v-model.lazy="name">
    {{name}}
</div>

trim

trim; cut off, here means: remove all spaces at both ends

<div class="app">
    <input type="text" v-model.trim="name">
    {{name}}
</div>

number

Generally used for types that can be expressed in numbers, such as age and price

<div class="app">
    <input type="text" v-model.number="age">
    {{age}}
</div>

In normal cases, if number is not provided, the user input is a number, but it is also a string of numbers. If the modifier v-model.number is filled in here, then the number obtained is of type number and no further conversion is required.

In addition to being used in input boxes whose type is text, v-model can also be used in other places.

Use of v-model on different input types and other elements

1. In addition to the input element, it can also be used on other types of input elements

1.1 Use on input element type radio (single selection box)

<div class="app">
	<label>
		Male <input type="radio" v-model="sex" value="male">
	</label>
	<label>
		Female <input type="radio" v-model="sex" value="famale">
	</label>
	{{sex}}
</div>
//main.js
var app = new Vue({
	el:'.app',
	data:{
		sex:'',
	}
});

1.2 Use on input element type checkbox (checkbox)

<div class="app">
	Do you like men or women:<br>
	<label>
		Male <input type="checkbox" v-model="sex" value="male">
	</label>
	<label>
		Female <input type="checkbox" v-model="sex" value="famale">
	</label><br>
	{{sex}}
</div>
var app = new Vue({
	el:'.app',
	data:{
		sex:['male'],
	}
});

2. Use of v-model in textarea (multi-line text)

Multi-line text

In fact, there is no difference in the usage of multi-line text and single-line text, except that one is multi-line and the other is single-line, and the usage is the same.

<div class="app">
	<textarea v-model="article"></textarea>
</div>
var app = new Vue({
	el:'.app',
	data:{
		article:`has a lot of code. . . . . . . . . . . . . . . . . . . . `,
	}
});

3. Use of v-model in select (drop-down list)

3.1 Single-select drop-down list

<div class="app">
	<div>where are you from? </div>
	<select v-model='from'>
		<option value="1">GUANGZHOU</option>
		<option value="2">BEIJING</option>
	</select>
</div>
var app = new Vue({
	el:'.app',
	data:{
		from:'1',
	}
});

3.2 Multi-select drop-down list

In fact, it is to add a multiple attribute to the element, indicating multiple, multiple selection

<div class="app">
	<div>where are you want to go? </div>
	<select v-model='from' multiple>
		<option value="1">GUANGZHOU</option>
		<option value="2">BEIJING</option>
		<option value="4">SHANGHAI</option>
		<option value="5">CHENGDU</option>
	</select>
</div>

var app = new Vue({
	el:'.app',
	data:{
		from:['1','2'],
	}
});

Summarize

This is the end of this article about v-model and its modifiers. For more information about v-model and its modifiers, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the modifiers of the v-model directive in Vue.js
  • Usage instructions for Vue's v-model modifiers .lazy, .number and .trim
  • Detailed explanation of the difference between v-model directive and .sync modifier in Vue

<<:  Teach you how to solve the error when storing Chinese characters in MySQL database

>>:  Design of image preview in content webpage

Recommend

Installation and configuration of mysql 8.0.15 under Centos7

This article shares with you the installation and...

CSS border adds four corners implementation code

1.html <div class="loginbody"> &l...

Mysql query the most recent record of the sql statement (optimization)

The worst option is to sort the results by time a...

Gojs implements ant line animation effect

Table of contents 1. Gojs Implementation 1. Drawi...

How to implement Ajax concurrent request control based on JS

Table of contents Preface Ajax serial and paralle...

MySql 5.7.21 free installation version configuration method under win10

1. Unzip to the location where you want to instal...

How to use HTML 5 drag and drop API in Vue

The Drag and Drop API adds draggable elements to ...

Windows 10 installation vmware14 tutorial diagram

Software Download Download software link: https:/...

In-depth analysis of HTML semantics and its related front-end frameworks

About semantics Semantics is the study of the rel...

Tutorial on configuring and using i3 window manager in Linux

In this article, I will show you how to install a...

Complete steps to install MySQL 5.5 on CentOS

Table of contents 1. Preparation before installat...

Example of using CSS3 to customize the style of input multiple-select box

Principle: First hide the input element, then use...

Detailed explanation of primary keys and transactions in MySQL

Table of contents 1. Comments on MySQL primary ke...