Home > Web Front-end > Vue.js > Learn about 16 Vue instructions from scratch to manually encapsulating custom instructions

Learn about 16 Vue instructions from scratch to manually encapsulating custom instructions

WBOY
Release: 2022-01-30 06:00:31
forward
4468 people have browsed it

This article brings you relevant knowledge about Vue instructions and manual encapsulation of custom instructions. I hope it will be helpful to you.

Learn about 16 Vue instructions from scratch to manually encapsulating custom instructions

In the front-end basic interview, Vue’s instructions are considered a high-frequency interview question

The interviewer asked: What instructions does Vue have?

Just tell him: As of Vue3.2, Vue 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 , among which v-memo is new in 3.2, v-is is abandoned in 3.1.0

If the interviewer further asks: How to encapsulate a custom instruction?

Just tell him: Custom instructions are divided into global custom instructions and local instructions; in Vue3, you can register a global custom instruction through directive() on the application instance. If you want to register a local instruction, you can Configure the directives option in the component to register local instructions

After reading this article, you will fully understand the 16 Vue instructions and master how to customize a directive

1. Introduction

1.1 What is a Vue directive

In Vue, a directive is actually a special attribute

Vue will do something behind the scenes based on the directive. As for what to do specifically, Vue will perform different operations according to different instructions. The details will be discussed later

1.2 What are the characteristics

Vue An obvious feature of instructions is that they all start with v-, for example: v-text

<span v-text="msg"></span>
Copy after login

2. Built-in instructions

2.1 What are the built-in instructions in Vue?

Built-in instructions refer to Vue’s own instructions, which can be used out of the box.

Vue 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, among which v-memo is new in 3.2, v-is is abandoned in 3.1.0

Let’s take a look at these built-in instructions Basic usage

2.2 Understand the basic usage of the 16 built-in instructions

2.2.1 v-text

v-text is used to update the textContent of the element, for example:

<h1 v-text="msg"></h1>
Copy after login

The content of the h1 element ultimately depends on the value of msg

Learn about 16 Vue instructions from scratch to manually encapsulating custom instructions

2.2.2 v-html

is very similar to v-text, except that v-html is used to update the innerHTML of the element, such as

<div v-html="&#39;<h1>Hello LBJ</h1>&#39;"></div>
Copy after login

Learn about 16 Vue instructions from scratch to manually encapsulating custom instructions

It should be noted that the content inside must be inserted as ordinary HTML

2.2.3 v-show

v-show can be based on the true value of the expression. False value, switches the display value of the element, used to control the display and hiding of the element, for example:

Learn about 16 Vue instructions from scratch to manually encapsulating custom instructions

It can be seen that when the conditions change, this instruction triggers display or hiding Transition effects

Note: v-show does not support the