Home > Web Front-end > Vue.js > body text

What is a custom directive? Learn more about custom directives in Vue

青灯夜游
Release: 2022-06-02 21:08:20
forward
5220 people have browsed it

What is a custom directive? This article will take you to learn more about the custom instructions in Vue Essential Knowledge for Getting Started, introduce private custom instructions and global custom instructions, and see how to dynamically bind parameter values ​​​​for custom instructions. I hope to be helpful!

What is a custom directive? Learn more about custom directives in Vue

1. What is a custom instruction

vue officially provides v-text, v- Commonly used instructions such as for, v-model, v-if, etc. In addition, vue also allows developers to customize instructions. (Learning video sharing: vue video tutorial)

2. Classification of custom instructions

Auto in vue Definition instructions are divided into two categories, namely:

  • PrivateCustom instructions
  • GlobalCustom instructions

3. Private custom directives

In each vue component, you can use directives Declare private custom instructions under the node. The sample code is as follows:
What is a custom directive? Learn more about custom directives in Vue

4. Use custom instructions

When using custom instructions, you need to add v- prefix. The sample code is as follows:
What is a custom directive? Learn more about custom directives in Vue

5. Dynamically bind parameter values ​​for custom instructions

in template When using a custom instruction in the structure, you can use the equal sign (=) to dynamically bind the parameter value to the current instruction :
What is a custom directive? Learn more about custom directives in Vue

6. Obtain the parameter value of the instruction through binding

When declaring a custom instruction, you can use the formal parameter

The second parameter, to receive the parameter value of the command:
What is a custom directive? Learn more about custom directives in Vue

7, update function

bind Function is only called once : called when the instruction is bound to the element for the first time, the bind function will not be triggered when the DOM is updated . update The function will be called every time the DOM is updated. The sample code is as follows:
What is a custom directive? Learn more about custom directives in Vue

8. Function abbreviation If

bind

and The logic in the update function is exactly the same, so the custom instructions in Object format can be abbreviated as Function format:
What is a custom directive? Learn more about custom directives in Vue

9. Global custom instructionsGlobally shared custom instructions need to be carried out through "

Vue.directive()" Statement, the sample code is as follows:
What is a custom directive? Learn more about custom directives in Vue (Learning video sharing:

web front-end development

, Basic programming video)

The above is the detailed content of What is a custom directive? Learn more about custom directives in Vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template