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>...</a> <!-- 缩写 --> <a>...</a> <p>标题</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>...</a> <!-- 缩写 --> <a>...</a> //是一个方法名 <p>一段文本</p> <button>点击隐藏文本</button> //直接是一个内联的语句 <button>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> </p><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>阻止冒泡</p> //不用通过$event事件再来写了
Some commonly used modifiers are:
• .stop
• .prevent
• .capture
• .self
• .once
< !一阻止单击事件冒泡一〉 <a></a> 〈!一修饰符可以串联一〉 <a></a> 〈!一添加事件侦听器时使用事件捕获模式一〉 <p> ... </p> 〈!一只当事件在该元素本身(而不是子元素) 触发时触发回调一〉 <p> ... </p> < !一只触发一次,组件同样适用一〉 <p> ... </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><p style="text-align: left;">3. v-model: two-way binding of data; used for form input, etc.; for example: </p><p style="text-align: left;">4. v-show: conditional rendering instruction, set the css style attribute for DOM</p><p style="text-align: left;">5. v-if: conditional rendering instruction, dynamically added in DOM Or delete DOM elements</p><p style="text-align: left;">6. v-else: conditional rendering instruction, must be used in pairs with v-if</p><p style="text-align: left;">7. v-else-if: judge multi-layer conditions, must be paired with v -if used in pairs; </p><p style="text-align: left;">8, v-text: Update the textContent of the element; for example: <span v-text="msg"></span> is equivalent to {{ msg}} span>;</p><p style="text-align: left;">9. v-html: Update the innerHTML of the element; the tag name will also be included. </p><p style="text-align: left;">10. v-for: loop instruction; for example: </p><pre class="brush:php;toolbar:false"><p> </p>
- { { book.name } }
10.1 v-for expressionSupports an optional parameter as the index of the current item when traversing the array , For example:
<p> </p>
- {{ index}} - {{book.name })
10.2 v-For expressionWhen traversing the object attributes, there are two optional parameters, namely key name and index:
<p> </p>
- { { index } } - { { key } } : { { value } }
10.3 v - The expression of for can also iterate integers:
<p> <span>{{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> </p>
- 书名:{{book.name}}
- 作者:{{book.author}}
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!

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.


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

SublimeText3 Chinese version
Chinese version, very easy to use

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

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.