Vue Advanced Tutorial: Detailed Explanation of v-model
Share
The explanation on v-model
on the Vue official website tutorial is not very detailed. The purpose of writing this article is to analyze it in detail and introduce Vue 2.2 v-model
The improvements, and then interspersed with a little knowledge of Vue.
In Vue, there are many methods similar to Angular, mainly because Angular was the source of inspiration for Vue’s early development. However, there are many problems in Augular that have been solved in Vue.
v-model When used on input elements
v-model
Although it is very similar to Angular using two-way data binding ng- model
, but Vue is a single data flow, v-model
is just syntactic sugar: ↓
<input v-model="sth" /> <input v-bind:value="sth" v-on:input="sth = $event.target.value" />
The first line of code is actually just syntactic sugar for the second line. Then the second line of code can be abbreviated like this: ↓
<input :value="sth" @input="sth = $event.target.value" />
To understand this line of code, first you need to know that the input
element itself has an oninput event, which is newly added to HTML5. Similar to onchange
, whenever the content of the input box changes, oninput
will be triggered and the latest value
will be passed to sth
.
If you don’t know where $event comes from, then you need to click on it and review the documentation.
We carefully observe the two lines of code of syntactic sugar and original syntax, and we can draw a conclusion:
When adding the v-model attribute to the element, the default value as an attribute of the element, and then use the 'input' event as a trigger event to deliver the value in real time
v-model When used on a component
v-model
It can be used not only on input
, but also on components. The following is an example similar to the Vue official website tutorial (we have to consider two issues when looking at this example):

The initial value of price
of the parent component is 100. Changing the value of the child component can update the parent component in real time. ’s price
<div id="demo"> <currency-input v-model="price"></currentcy-input> <span>{{price}}</span></div><script src="https://cdn.bootcss.com/vue/2.3.0/vue.js?1.1.11"></script><script>Vue.component('currency-input', { template: ` <span> <input ref="input" :value="value" <!--为什么这里把 'input' 作为触发事件的事件名?`input` 在哪定义的?--> @input="$emit('input', $event.target.value)" > </span> `, props: ['value'],// 为什么这里要用 value 属性,value在哪里定义的?貌似没找到啊? })var demo = new Vue({ el: '#demo', data: { price: 100, } })</script>
If you know the answers to these two questions, then congratulations on your true mastery of v-model
, if you don’t understand, then You can take a look at this code: ↓
<currency-input v-model="price"></currentcy-input><!--上行代码是下行的语法糖 <currency-input :value="price" @input="price = arguments[0]"></currency-input> -->
Now you know where value
and input
come from. Similar to what is summarized above:
When adding the v-model attribute to a component, the value will be used as the attribute of the component by default, and then the 'input' value will be used as the event name when binding events to the component
Disadvantages and solutions of v-model
When creating common components like check boxes or radio buttons, v-model
is not easy to use.
<input type="checkbox" v-model="sth" />
v-model
has provided us with value
attributes and oninput
events, but what we need is not value
attribute, but the checked
attribute, and when you click this radio button, it will not trigger the oninput
event, it will only trigger the onchange
event. This is embarrassing
The above is the detailed content of Vue Advanced Tutorial: Detailed Explanation of v-model. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Dewu APP is currently a very popular brand shopping software, but most users do not know how to use the functions in Dewu APP. The most detailed usage tutorial guide is compiled below. Next is the Dewuduo that the editor brings to users. A summary of function usage tutorials. Interested users can come and take a look! Tutorial on how to use Dewu [2024-03-20] How to use Dewu installment purchase [2024-03-20] How to obtain Dewu coupons [2024-03-20] How to find Dewu manual customer service [2024-03-20] How to check the pickup code of Dewu [2024-03-20] Where to find Dewu purchase [2024-03-20] How to open Dewu VIP [2024-03-20] How to apply for return or exchange of Dewu

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

PhotoshopCS is the abbreviation of Photoshop Creative Suite. It is a software produced by Adobe and is widely used in graphic design and image processing. As a novice learning PS, let me explain to you today what software photoshopcs5 is and how to use photoshopcs5. 1. What software is photoshop cs5? Adobe Photoshop CS5 Extended is ideal for professionals in film, video and multimedia fields, graphic and web designers who use 3D and animation, and professionals in engineering and scientific fields. Render a 3D image and merge it into a 2D composite image. Edit videos easily

1. First open WeChat. 2. Click [+] in the upper right corner. 3. Click the QR code to collect payment. 4. Click the three small dots in the upper right corner. 5. Click to close the voice reminder for payment arrival.

With the continuous development of smart phones, the functions of mobile phones have become more and more powerful, among which the function of taking long pictures has become one of the important functions used by many users in daily life. Long screenshots can help users save a long web page, conversation record or picture at one time for easy viewing and sharing. Among many mobile phone brands, Huawei mobile phones are also one of the brands highly respected by users, and their function of cropping long pictures is also highly praised. This article will introduce you to the correct method of taking long pictures on Huawei mobile phones, as well as some expert tips to help you make better use of Huawei mobile phones.

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus
