This time I will bring you Vue’s built-in command methods and events. What are the precautions for using Vue’s built-in command methods and events? Here 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 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 to listen to DOM events; For example: v -on:click v-on:keyup
By the way, let’s talk about methods and events
2.1 The expression of @click can directly use JavaScript statements , or it can be a function name in the methods option of 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 can be used For accessing native DOM events, you can prevent event 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 Modifier:
In @binding Add a small dot "." after the 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 expression supports an optional parameter as the index of the current item when traversing the array, for example:
<p> </p>
- {{ index}} - {{book.name })
10.2 When the expression of v- for traverses the object properties, there are two optional parameters, namely key name and index:
<p> </p>
- { { index } } - { { key } } : { { value } }
10.3 The expression of v- 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 be updated immediately.
• 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
When you do not want to change the original array and want to filter or sort the display through a copy of the array, you can use calculated properties to return the filtered or sorted array. ,For example:
<p> </p>
- 书名:{{book.name}}
- 作者:{{book.author}}
11、v-cloak:不需要表达式,这个指令保持在元素上直到关联实例结束编译; v-cloak 是一个解决初始化慢导致页面闪动的最佳实践 ;
12、v-once:也是一个不需要表达式的指令,作用是定义它的元素或组件只渲染一次,包括元素或组件的所有子节点。
首次渲染后,不再随数据的变化重新渲染,将被视为静态内容; v-once 在业务中也很少使用,当你需要进一步优化性能时,可能会用到。
13、v-pre:不需要表达式,跳过这个元素以及子元素的编译过程,以此来加快整个项目的编译速度;例如: {{ this will not be compiled }} span>
;
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Vue built-in command methods and events. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

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.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools