1. lazyThe lazy modifier is used to change the value of the input box. The value of the v-model binding will not change when the cursor leaves the input box. <input type="text" v-model.lazy="value"> <div>{{value}}</div> data() { return { value: '111111' } } 2.trimThe trim modifier is similar to the trim() method in JavaScript, which filters out the leading and trailing spaces of the value bound to v-model. <input type="text" v-model.trim="value"> <div>{{value}}</div> data() { return { value: '111111' } } 3.numberThe function of the number modifier is to convert the value into a number, but there are two different situations: entering the string first and entering the number first. <input type="text" v-model.number="value"> <div>{{value}}</div> data() { return { value: '111111' } } If you enter numbers first, only the first part of the numbers will be taken. If you enter letters first, the number modifier will be invalid. 4.stopThe stop modifier is used to stop bubbling <div @click="clickEvent(2)" style="width:300px;height:100px;background:red"> <button @click.stop="clickEvent(1)">Click</button> </div> methods: { clickEvent(num) { // Click the button without stop to output 1 2 // Added stop and clicked the button to output 1 console.log(num) } } 5. captureBy default, events bubble from the inside out. The capture modifier works the other way around, capturing events from the outside. <div @click.capture="clickEvent(2)" style="width:300px;height:100px;background:red"> <button @click="clickEvent(1)">Click</button> </div> methods: { clickEvent(num) { // Without capture, click the button to output 1 2 // Added capture and clicked the button to output 2 1 console.log(num) } } 6.selfThe self modifier is used to trigger the event only when the event binding itself is clicked. <div @click.self="clickEvent(2)" style="width:300px;height:100px;background:red"> <button @click="clickEvent(1)">Click</button> </div> methods: { clickEvent(num) { // Without adding self, click the button to output 1 2 // Added self. Clicking the button will output 1. Clicking the div will output 2. console.log(num) } } 7.onceThe once modifier is used to execute the event only once. <div @click.once="clickEvent(2)" style="width:300px;height:100px;background:red"> <button @click="clickEvent(1)">Click</button> </div> methods: { clickEvent(num) { // Without once, click the button multiple times to output 1 // Added once. Clicking the button multiple times will only output 1 once console.log(num) } } 8.PreventThe prevent modifier is used to prevent default events (such as the jump of the a tag) <a href="#" rel="external nofollow" @click.prevent="clickEvent(1)">Click me</a> methods: { clickEvent(num) { // Without prevent, click on label a to jump first and then output 1 // Added prevent, clicking on label a will not jump but will only output 1 console.log(num) } } 9.nativeThe native modifier is added to the event of the custom component to ensure that the event can be executed Cannot execute <My-component @click="shout(3)"></My-component> Can execute <My-component @click.native="shout(3)"></My-component> 10.left, right, middleThese three modifiers are events triggered by the left, middle and right buttons of the mouse <button @click.middle="clickEvent(1)" @click.left="clickEvent(2)" @click.right="clickEvent(3)">Click me</button> methods: { // Click the middle button to output 1 // Click the left button to output 2 // Right click to output 3 clickEvent(num) { console.log(num) } } 11. PassiveWhen we listen to the scroll event of an element, the onscroll event will be triggered continuously. This is no problem on the PC side, but on the mobile side, it will make our web page become stuck. Therefore, when we use this modifier, it is equivalent to giving the onscroll event a .lazy modifier. <div @scroll.passive="onScroll">...</div> 12. camelWithout camel viewBox, it will be recognized as viewbox <svg :viewBox="viewBox"></svg> Only after adding canmel viewBox will it be recognized as viewBox <svg :viewBox.camel="viewBox"></svg> 12.syncWhen a parent component passes a value to a child component, and the child component wants to change the value, it can do this In the parent component <children :foo="bar" @update:foo="val => bar = val"></children> In the subcomponent this.$emit('update:foo', newValue) The function of the sync modifier is to abbreviate: In the parent component <children :foo.sync="bar"></children> In the subcomponent this.$emit('update:foo', newValue) 13.keyCodeWhen we write the event like this, the event will be triggered no matter which button is pressed <input type="text" @keyup="shout(4)"> So what if you want to limit it to a certain button trigger? This is where the keyCode modifier comes in handy <input type="text" @keyup.keyCode="shout(4)"> KeyCode provided by Vue: //Ordinary key.enter .tab .delete //(captures the "delete" and "backspace" keys) .space .esc .up .down .left .right //System modifier key.ctrl .alt .meta .shift For example: Press ctrl to trigger <input type="text" @keyup.ctrl="shout(4)"> You can also use mouse events + buttons <input type="text" @mousedown.ctrl.="shout(4)"> Can trigger with multiple keys, such as ctrl + 67 <input type="text" @ This concludes this article about the 13 most frequently asked Vue modifiers in interviews. For more relevant Vue modifiers, please search 123WORDPRESS.COM’s previous articles or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: VM VirtualBox virtual machine mount shared folder
>>: Detailed explanation of Nginx timeout configuration
Table of contents What is a trigger Create a trig...
When is the table used? Nowadays, tables are gene...
Main library execution CREATE DATABASE test CHARA...
<br />Related articles: Web skills: Multiple...
1. Install dependency packages yum -y install gcc...
Table of contents 1. Repeated declaration 1.1 var...
VMWare (Virtual Machine ware) is a "virtual ...
Preface If you are like me, as a hard-working Jav...
The docker image id is unique and can physically ...
Table of contents What is the Linux system that w...
If you want to install multiple tomcats, you must...
We use the translate parameter to achieve movemen...
I am going to review Java these two days, so I wr...
Preface This article is just a simple record of m...
[Looking at all the migration files on the Intern...