Instructions for using vue built-in instructions
This time I will bring you the instructions for using the built-in instructions of vue. What are the precautions of the instructions for using the built-in instructions of vue. The following is a practical case, let’s take a look.
Directives are special attributes with v- prefix. Their responsibility is to reactively apply the associated effects to the DOM when the value of the expression changes.
Built-in instructions
1. v-bind: respond to and update DOM characteristics; for example: v-bind:href v-bind:class v-bind:title etc.
The main usage is to bind attributes and dynamically update the attributes on HTML elements;
<a v-bind:href="url" rel="external nofollow" rel="external nofollow" >...</a> <!-- 缩写 --> <a :href="url" rel="external nofollow" rel="external nofollow" >...</a> <p :title='title'>标题</p> var app = new Vue({ el: '#app', data: { url: 'www.baidu.com', title: 'bind' }, })
2, v-on: used for monitoring DOM events; For example: v-on:click v-on:keyup
By the way, let’s talk about methods and events
2.1 Expression of @click The formula can use JavaScript statements directly, or it can be a function name in the methods option in the Vue instance. Parameters can be passed in the method
<!-- 完整语法 --> <a v-on:click="doSomething">...</a> <!-- 缩写 --> <a @click="doSomething()">...</a> //是一个方法名 <p ng-if='show'>一段文本</p> <button @click="show=false">点击隐藏文本</button> //直接是一个内联的语句 <button v-on:click="count++">Add 1</button> var app = new Vue({ el: '#app', data:{ show: true, counter: 0 }, methods: { doSomething: function(){ console.log(this.title); }, } })
2.2 Methods and events:
Vue provides a special variable $event, which is used to access native DOM events, which can prevent events from bubbling or prevent links from opening.
Write an example to prevent bubbling:
<p @click="stopClick1('stop1',$event)"> <p @click="stopClick2('stop2',$event)"> <p @click="stopClick3('stop3',$event)">阻止冒泡</p> </p> </p> </p> methods:{ stopClick3: function(message, event){ console.log(message); event.stopPropagation(); //阻止冒泡 }, stopClick2: function(message, event){ console.log(message); }, stopClick1: function(message, event){ console.log(message); } }
2.3 Modification Symbol:
Add a small dot "." after the @bound event, and then follow it with a suffix to use the modifier.
The above bubbling event can be written as a direct user modifier:
<p @click.stop="stopClick3('stop3')">阻止冒泡</p> //不用通过$event事件再来写了
Some commonly used modifiers are:
• .stop
• .prevent
• .capture
• .self
• .once
< !一阻止单击事件冒泡一〉 <a @click.stop=”handle "></a> 〈!一修饰符可以串联一〉 <a @click.stop.prevent=” handle ” ></a> 〈!一添加事件侦听器时使用事件捕获模式一〉 <p @click . capture=”handle ”> ... </p> 〈!一只当事件在该元素本身(而不是子元素) 触发时触发回调一〉 <p @click.self=” handle ”> ... </p> < !一只触发一次,组件同样适用一〉 <p @click.once=” handle ”> ... </p>
When monitoring keyboard events on a form element, also You can use key modifiers, such as calling a method only when a specific key is pressed:
< !一只有在keyCode 是13 时调用vm.submit()一〉 <input @keyup.13 =“ submit ”〉
3. v-model: two-way binding of data; used for form input, etc.; for example: < input v-model = "message">
4. v-show: conditional rendering instruction, set the css style attribute for DOM
5. v-if: conditional rendering instruction, dynamically added in DOM Or delete DOM elements
6. v-else: conditional rendering instruction, must be used in pairs with v-if
7. v-else-if: judge multi-layer conditions, must be paired with v -if used in pairs;
8, v-text: Update the textContent of the element; for example: is equivalent to < span>{{ msg}} span>;
9. v-html: Update the innerHTML of the element; the tag name will also be included.
10. v-for: loop instruction; for example:
<p id= "app "> <ul> <li v-for="book in books">{ { book.name } }</li> </ul> </p> var app =new Vue({ el: '#app', data: { books: [ {name: '<vue.js实战>'}, {name: '<javascript语言精粹>'}, {name: '<javascript高级程序设计>'} ] } });
10.1 v-for expressionSupports an optional parameter as the index of the current item when traversing the array , For example:
<p id="app"> <ul> <li v-for="(book , index) in books ">{{ index}} - {{book.name })</li> </ul> </p>
10.2 v-For expressionWhen traversing the object attributes, there are two optional parameters, namely key name and index:
<p id= "app"> <ul> <li v-for="(value , key , index) in user "> { { index } } - { { key } } : { { value } } </li> </ul> </p> var app = new Vue({ el: '#app', data: { name: 'Aresn', grender: '男', age:23 } });
10.3 v - The expression of for can also iterate integers:
<p id="app"> <span v-for="n in 10">{{n}}</span> </p>
10.4 Array update
When we modify the array, Vue will detect the data change, so the view rendered with v-for will also immediately renew.
• push()
• pop()
• shift()
• unshit()
• splice()
• sort()
• reverse ()
These methods will change the original array called by these methods
For example, we will add an item to the data books of the previous example:
app.books.push({ name: '《css世界》' });
Some methods will not Change the original array, for example:
• filter()
• concat()
• slice()
They return a new array. When using these non-mutation methods When Vue detects changes in the array, it does not directly re-render the entire list, but maximizes the reuse of DOM elements.
In the replaced array, items containing the same elements will not be re-rendered, so you can boldly replace the old array with a new array without worrying about performance issues.
10.5 Filtering and Sorting
当你不想改变原数组,想通过一个数组的副本来做过滤或排序的显示时, 可以使用计算属性来返回过滤或排序后的数组 ,例如:
<p id="app"> <ul> <template v-for="book in filterBooks"> <li>书名:{{book.name}}</li> <li>作者:{{book.author}}</li> </template> </ul> </p> var app= new Vue({ el: '#app', computed: { filterBooks: function(){ return this.books.filter(function (book) { return book.name.match(/JavaScript/); }) }, } });
11、v-cloak:不需要表达式,这个指令保持在元素上直到关联实例结束编译; v-cloak 是一个解决初始化慢导致页面闪动的最佳实践 ;
12、v-once:也是一个不需要表达式的指令,作用是定义它的元素或组件只渲染一次,包括元素或组件的所有子节点。
首次渲染后,不再随数据的变化重新渲染,将被视为静态内容; v-once 在业务中也很少使用,当你需要进一步优化性能时,可能会用到。
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Instructions for using vue built-in instructions. 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



How to get items using commands in Terraria? 1. What is the command to give items in Terraria? In the Terraria game, giving command to items is a very practical function. Through this command, players can directly obtain the items they need without having to fight monsters or teleport to a certain location. This can greatly save time, improve the efficiency of the game, and allow players to focus more on exploring and building the world. Overall, this feature makes the gaming experience smoother and more enjoyable. 2. How to use Terraria to give item commands 1. Open the game and enter the game interface. 2. Press the "Enter" key on the keyboard to open the chat window. 3. Enter the command format in the chat window: "/give[player name][item ID][item quantity]".

This article aims to help beginners quickly get started with Vue.js3 and achieve a simple tab switching effect. Vue.js is a popular JavaScript framework that can be used to build reusable components, easily manage the state of your application, and handle user interface interactions. Vue.js3 is the latest version of the framework. Compared with previous versions, it has undergone major changes, but the basic principles have not changed. In this article, we will use Vue.js instructions to implement the tab switching effect, with the purpose of making readers familiar with Vue.js

Mobile devices have become an essential part of people's lives in modern society. Games have also become one of the main forms of entertainment in people's spare time. There are constantly people working on developing new tools and technologies to optimize gameplay and improve the gaming experience. The input method with its own MC command is one of the eye-catching innovations. And how it can bring a better gaming experience to players. This article will delve into the infinite possibilities of the built-in MC command input method. Introduction to the built-in MC command input method. The built-in MC command input method is an innovative tool that combines the functions of MC commands and intelligent input methods. This enables more operations and functions. By installing the input method on a mobile device, players can easily use various commands in the game. Enter commands quickly to improve game efficiency

1. Preface A few days ago, a fan named [emerson] asked a question about Python sorting in the Python diamond exchange group. I will share it here with everyone and learn together. In fact, [Teacher Yu Liang], [Eternity in Budapest] and others have talked a lot here, but it is still a bit difficult for friends with poor foundation. However, the built-in function sorted() is still used a lot in practical applications. I will talk about it here separately. I hope that next time friends encounter it, they will not panic. 2. Basic usage The built-in function sorted() can be used for sorting. The basic usage is very simple. Take an example as shown below. lst=[3,28,18,29,2,5,88

1. Switch between noise reduction mode and transparency mode. Press and hold the handle of the earphones for about 1 second to switch between noise reduction mode and transparency mode. 2. In music mode, press the earphone handle once to pause or play music. Press the earphone handle twice to play the next song. Press the earphone handle three times to play the previous song or wake up the voice. 3. In call mode, during a call, press the earphone handle once to answer or hang up the call. 4. How to reset Open the earphone box. When the charging box indicator light flashes red 5 times, release the button and the earphones are reset. 3. How to connect the phone 1. Open the charging box 2. Press and hold the setting button for 2 seconds 3. When a pop-up window appears on the phone screen, click to confirm the connection. 4. How to check the battery status 1. When the earphones are connected to the mobile phone, you can check the battery level of the earphones and charging box in the pop-up window on the mobile phone screen. 2,

The instructions that the computer can directly execute include operation codes and operands. The opcode refers to the part of the instruction or field specified in the computer program to perform the operation. It is actually the instruction sequence number, which is used to tell the CPU which instruction needs to be executed.

Instructions for accessing and using the payment function of UniApp. With the popularity of mobile payment, many applications need to integrate payment functions to facilitate users to make online payments. As a cross-platform development framework based on Vue.js, UniApp has the characteristics of one-time development and multi-platform use, and can easily implement the payment function. This article will introduce how to access the payment function in UniApp and give code examples. 1. To access the payment function, add payment permissions in the manifest.json file on the App side:

Instructions are commands that control computer execution, and they consist of operation codes and address codes. Usually an instruction includes two aspects: operation code and operand (address code). The operation code determines the operation to be completed, and the operand refers to the data participating in the operation and the address of the unit where it is located.
