1. Why define custom directives
After studying the Vue front-end framework, we also know many built-in instructions, such as v-for instruction, v-if instruction, v-bind instruction, v-model instruction and so on. However, because these built-in instructions tend to be instrumental, and sometimes it is necessary to implement specific business logic, the application of these built-in instructions can not achieve some specific functions. In this regard, Vue JS allows users to customize instructions to facilitate the repeated processing of DOM elements, so as to improve the reusability of code. A user-defined instruction is an instruction registered by the user on the premise that the specific conditions of the built-in instruction are met. Just like the user-defined function in C, some specific conditions need to be met when creating the user-defined function, otherwise an error will occur.
2. Register custom directives
Vue.js provides a method to register user-defined instructions. Global user-defined instructions and local user-defined instructions can be registered through different methods.
2.1 register global customization instructions
Via Vue The directive (ID, definition) method can register a global custom instruction. This method can receive two parameters: instruction ID and definition object. The instruction ID is the unique identifier of the instruction, and the definition object is the hook function of the defined instruction. For example, register a global custom instruction, which enables the element to automatically get the focus when the page is loaded. The code is as follows:
In the above code, focus is the user-defined instruction ID, excluding the v-prefix, and inserted is the hook function in the instruction definition object. The hook function indicates that when the bound element is inserted into the parent node, the element will automatically obtain the focus. After registering the global instruction, the corresponding function can be realized by applying the instruction in the bound element. The operation results are shown in the figure below:
2.2 register local customization instructions
You can register a local custom directive through the directives option in the Vue instance. For example, register a local custom instruction, which can add borders to elements. The code is as follows:
In the above code, the small hump naming method is adopted when registering the local custom instruction, and the custom instruction ID is defined as addBorder, while the writing method when applying the instruction in the element is v-add-border. This is recommended when naming custom directives. The operation results are shown in the figure below:
Note: local custom instructions can only be invoked in the current instance, but cannot be invoked in other instances. Just like the local variable in C, it can only be used under the current function and cannot be used under other functions.
3. Hook function
3.1 what is a hook function
1. Hook function: a hook function captures an event in the system when it is triggered, and then performs some operations. Is a program for processing system messages. "Hook" means to give you a chance to do some processing at a certain stage.
Hook function is a function that is called by the system when the system message is triggered; Not triggered by the user.
3.2 common hook functions
1. When registering an instruction, you can pass in the definition definition object to give some special functions to the instruction. An instruction definition object can provide the following types of hook functions.
The above hook functions are optional. Each hook function can pass in el, binding and vnode parameters. The componentUpdated and update hook functions can also pass in oldVnode parameters. These parameters are described as follows:
- el: the element bound by the instruction, which can be used to directly operate DOM
- binding: an object that contains many attributes.
2. The binding parameter object contains properties
- vnode: virtual node generated by Vue compilation
- oldVnode: the previous virtual node, which is only available in the componentUpdated and update hook functions
All parameters except el parameter should be read-only and should not be modified
4. Binding value of custom instruction
4.1 binding numeric constants
Register a user-defined instruction, set the left position of the positioning element through the instruction, and set the binding value of the instruction to a value, which is the distance from the bound element to the left side of the page. The code is as follows:
The operation results are shown in the figure below:
4.2 binding string constant
Single quotes are required to set the binding value of a custom instruction to a string constant. For example, register a custom instruction, set the text color through the instruction, and set the binding value of the instruction to the string '0000FF', which is the color set by the bound element. The example code is as follows:
The operation results are shown in the figure below:
4.3 binding object literal
The operation results are as follows:
Note: Vue will be followed up later JS front-end framework: component, I hope you will pay more support and attention.