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!

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.