There are 16 built-in instructions: v-text, v-html, v-show, v-if, v-else, v-else-if, v-for, v-on, v-bind, v-model, v-slot, v-pre, v-cloak, v-once, v-memo, v-is; v-memo is new in 3.2, and v-is is abandoned in 3.1.0. v-show is used to control the display and hiding of elements, and v-if is used to conditionally render elements based on the true or false value of an expression.
The operating environment of this tutorial: windows7 system, vue3 version, DELL G3 computer.
1. What is a Vue command?
In Vue, a command is actually a special attribute.
Vue will do something behind the scenes based on the command. , as for what to do specifically, Vue will perform different operations according to different instructions. The specific
## instructions will be mentioned later. What are the characteristics
An obvious feature of Vue instructions is that they all start with v-, for example: v-text<span v-text="msg"></span>
2, built-in instructions
2.1 What are the built-in instructions in Vue?
The built-in instructions refer to the instructions that come with Vue and can be used out of the boxVue has a total of 16 built-in instructions, including: v-text, v-html, v-show, v-if, v-else, v-else-if, v-for, v -on, v-bind, v-model, v-slot, v-pre, v-cloak, v-once, v-memo, v-is, v-memo is new in 3.2, v-is is in 3.1 Obsolete in .0Let’s learn about the basic use of these built-in instructions2.2 Understand the basic use of the 16 built-in instructions
2.2.1 v-text
The role of v-text is to update the textContent of the element, for example:<h1 v-text="msg"></h1>The content of the h1 element is final Depends on the value of msg
is very similar to v-text, except that v-html uses For updating the innerHTML of the element, for example
<div v-html="'<h1>Hello LBJ</h1>'"></div>
It should be noted that the content inside must be inserted as ordinary HTML
v-show can switch the display value of the element according to the true or false value of the expression, which is used to control the display and hiding of the element, for example:
You can see that when the conditions change, this command triggers the transition effect of showing or hiding.
Note: v-show does not support the element, nor does it support v-else
2.2.4 v-ifv-if is used to conditionally render elements based on the true or false value of an expression
Compared with v-show Than, v-if is the destruction or reconstruction of the element when switching, rather than simply showing and hiding
You can see that when the expression is false, v-if is Destroy the element directly, while v-show only hides it visually
And v-if can be , if the element is , its content will be extracted as a conditional block
2.2.5 v-elsev-else does not require an expression, which means adding an "else block", which is equivalent to displaying the elements of v-if when v-if meets the conditions , otherwise display v-else elements, for example:
Note: the sibling element before v-else must have v-if or v-else-if
Similarly, it represents the "else if block" of v-if. Like v-else, the previous sibling element must have v -if or v-else-if, for example:
v-for one for Iterative instructions can render elements or template blocks multiple times based on source data, for example:
You can also specify aliases for array indexes or keys for objects
<div v-for="(item, index) in items"></div> <div v-for="(value, key) in object"></div> <div v-for="(value, name, index) in object"></div>2.2.8 v-on
v-on is used to bind events to elements, which can be abbreviated as: @
Modifier
- .stop - calls event.stopPropagation()
- .prevent - calls event.preventDefault()
- . capture - Use the capture mode when adding an event listener
- .self - Only trigger the callback if the event is triggered from the element itself to which the listener is bound
- .once - The callback is only triggered once
- .left - only fires when the left mouse button is clicked
- .right - only fires when the right mouse button is clicked
- . middle - only fires when the middle mouse button is clicked
.passive - { passive: true } 模式添加偵聽器
例如:
<!-- 停止冒泡 --> <button @click.stop="doThis"></button>
需要注意,用在普通元素上時,只能監(jiān)聽原生 DOM 事件。用在自定義元素組件上時,也可以監(jiān)聽子組件觸發(fā)的自定義事件
2.2.9 v-bind
v-bind用于綁定數(shù)據(jù)和元素屬性,可以縮寫為: 或.(當(dāng)使用 .prop 修飾符時),比如
<div :someProperty.prop="someObject"></div> <!-- 相當(dāng)于 --> <div .someProperty="someObject"></div>
v-bind的3個修飾符
.camel - 將 kebab-case attribute 名轉(zhuǎn)換為 camelCase
.prop - 將一個綁定強(qiáng)制設(shè)置為一個 DOM property。3.2+
.attr - 將一個綁定強(qiáng)制設(shè)置為一個 DOM attribute。3.2+
2.2.10 v-model
v-model限制于:
components
v-model的3個修飾符:
.lazy - 惰性更新,監(jiān)聽 change 而不是 input 事件
.number - 輸入字符串轉(zhuǎn)為有效的數(shù)字
.trim - 輸入首尾空格過濾
在表單控件或者組件上可以創(chuàng)建雙向綁定,例如:
2.2.11 v-slot
v-slot用于提供具名插槽或需要接收 prop 的插槽
可選擇性傳遞參數(shù),表示插槽名,默認(rèn)值default
2.2.12 v-pre
v-pre指令用于跳過這個元素及其子元素的編譯過程,例如:
可以看到里頭的東西沒有被編譯
2.2.13 v-cloak
v-cloak指令主要用于解決插值表達(dá)式在頁面閃爍問題
<div v-cloak> {{ message }} </div>
[v-cloak] { display: none; }
這樣div只會在編譯結(jié)束后顯示
2.2.14 v-once
v-once指令用于表示只渲染一次,當(dāng)要重新渲染,元素/組件及其所有的子節(jié)點(diǎn)將被視為靜態(tài)內(nèi)容并跳過
2.2.15 v-memo 3.2+
用于緩存一個模板的子樹
該指令接收一個固定長度的數(shù)組作為依賴值進(jìn)行記憶比對。如果數(shù)組中的每個值都和上次渲染的時候相同,則整個該子樹的更新會被跳過
<div v-memo="[valueA, valueB]"></div>
在重新渲染時,如果 valueA 與 valueB 都維持不變,那么對這個
2.2.16 v-is
已在 3.1.0 中廢棄,改用:is
<component :is="currentView"></component>
相關(guān)推薦:vue.js視頻教程
The above is the detailed content of What are the components of Vue's built-in instructions?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

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

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

Netflix uses React as its front-end framework. 1) React's componentized development model and strong ecosystem are the main reasons why Netflix chose it. 2) Through componentization, Netflix splits complex interfaces into manageable chunks such as video players, recommendation lists and user comments. 3) React's virtual DOM and component life cycle optimizes rendering efficiency and user interaction management.

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.

Netflix mainly uses React as the front-end framework, supplemented by Vue for specific functions. 1) React's componentization and virtual DOM improve the performance and development efficiency of Netflix applications. 2) Vue is used in Netflix's internal tools and small projects, and its flexibility and ease of use are key.

The methods to implement the jump of a tag in Vue include: using the a tag in the HTML template to specify the href attribute. Use the router-link component of Vue routing. Use this.$router.push() method in JavaScript. Parameters can be passed through the query parameter and routes are configured in the router options for dynamic jumps.

There are the following methods to implement component jump in Vue: use router-link and <router-view> components to perform hyperlink jump, and specify the :to attribute as the target path. Use the <router-view> component directly to display the currently routed rendered components. Use the router.push() and router.replace() methods for programmatic navigation. The former saves history and the latter replaces the current route without leaving records.

Pagination is a technology that splits large data sets into small pages to improve performance and user experience. In Vue, you can use the following built-in method to paging: Calculate the total number of pages: totalPages() traversal page number: v-for directive to set the current page: currentPage Get the current page data: currentPageData()
