Detailed explanation of Vue advanced construction properties

Detailed explanation of Vue advanced construction properties

1. Directive custom directive

In the code under the Vue framework, native DOM operations are rarely used. That is because native DOM operations are encapsulated into instructions in Vue. For example, the <div v-text="xxx"></div> instruction we saw earlier actually has the following internal operation:

div.innerText = xxx; //Of course, the div here is the obtained DOM element

Vue encapsulates native DOM operations into instructions. If an element wants to use the instruction, it can directly use it as an attribute in the element tag in the HTML template. It is simple and convenient and reduces duplication.

However, Vue cannot take all DOM operations into consideration and encapsulate them into corresponding instructions. Some DOM operations are not known until developers use them in practice. Therefore, Vue provides a way for users to customize instructions, which can be roughly divided into the following two types:

Custom instructions are similar to custom components and are also divided into global and local. The following takes the custom instruction vy for printing the character y as an example:

Global Directives

Global properties are registered in specific functions provided by Vue:

Vue.directive("y", {
  inserted: function(el) {
    el.addEventListener("click", () => console.log("y"));
  }
});

Local instructions

Can only be used in the template that defines the directive

It can be in the template attribute of the complete version

main.js

new Vue({
  template:`
    <div vy>
      <button>Click</button>
    </div>
  `,
  directives:{
    'y':{
      inserted: function(el) {
        el.addEventListener("click", () => console.log("y"));
      }
    }
  }
}).$mount("#app");

Or in the export default{} of the incomplete version of the .vue file

app.vue

<template>
    <div>
      <button vy>Click</button>
    </div>
</template>
<script>
    export default {
      directives:{
        'y':{
          inserted: function(el) {
            el.addEventListener("click", () => console.log("y"));
          }
        }
      }
    };
</script>

directiveOptions

There are 5 function attributes in the instruction object

{
    bind: function (el,info,vnode,oldVnode) {}, //Executed when the element is created in memory inserted: function (parameters as above) {}, //Executed when the element is inserted into the page update: function (parameters as above) {}, 
    componentUpdated: function (parameters as above) {}, 
    unbind: function (parameters as above) {} //Execute when the element disappears}

In the function attribute parameter, el refers to the element that calls the instruction, and info contains all the information. When needed, just look for it in info.

2. Mixin

The main purpose of mixins is to reduce the duplication of construction options. You can extract the repeated construction options into a separate *.js file, import it, and then mix it into the construction options through the mixins attribute.

minxin is a smart mixin that will be mixed intelligently based on the added mixins and the current options, rather than simply copying.

3. Extends inheritance

Inheritance and mixin have similar functions, but extend is more abstract. Both simplify the repetition of construction options. Extend can add its own defined properties as fixed properties based on the original Vue shell, and then use it to create a custom class MyVue that inherits Vue when creating a Vue object.

MyVue.js

const MyVue = Vue.extend({
    minxins: [log]
});

export default MyVue;

4. provide and inject

The parent provides common data or methods

{
    //...
    provide(){
        return {
            xx: this.changexx,
            yy: this.changeyy
        }
    },
    methods:{
        changexx(){
            //...
        },
        changeyy(){
            //...
        }
    }
}

Descendants inject things to modify data

{
    inject: ["changexx", "changeyy"]
}

This works a bit like the .sync modifier, but is more general.

The above is a detailed explanation of Vue's advanced structural properties. For more information about Vue's advanced structural properties, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Summary of the use of Vue computed properties and listeners
  • Dynamically modify one of the css attribute values ​​in vue
  • Computed properties and listening properties in Vue
  • How to use loop object properties and attribute values ​​in Vue
  • Vue implements setting different styles according to the attribute values ​​in data
  • vue solves the problem of being unable to set reactive properties for undefined values, empty values, or primitive values
  • A brief understanding of the difference between Vue computed properties and watch
  • Solve the problem of Vue assigning errors to attributes defined in mapState
  • Vue Construction Options - Advanced Usage Instructions

<<:  How to reset password after forgetting password in MySQL8 (MySQL old method doesn't work)

>>:  How to quickly build an FTP file service using FileZilla

Recommend

Analysis of two usages of the a tag in HTML post request

Two examples of the use of the a tag in HTML post...

In-depth understanding of Vue's data responsiveness

Table of contents 1. ES syntax getter and setter ...

Issues and precautions about setting maxPostSize for Tomcat

1. Why set maxPostSize? The tomcat container has ...

W3C Tutorial (11): W3C DOM Activities

The Document Object Model (DOM) is a platform, a ...

CSS sets Overflow to hide the scroll bar while allowing scrolling

CSS sets Overflow to hide the scroll bar while al...

Angular framework detailed explanation of view abstract definition

Preface As a front-end framework designed "f...

Installation tutorial of MySQL 5.1 and 5.7 under Linux

The operating system for the following content is...

In-depth understanding of Worker threads in Node.js

Table of contents Overview The history of CPU-bou...

How to turn off eslint detection in vue (multiple methods)

Table of contents 1. Problem Description 2. Probl...

React useMemo and useCallback usage scenarios

Table of contents useMemo useCallback useMemo We ...

Zabbix redis automatic port discovery script returns json format

When we perform automatic discovery, there is alw...

Vue.js implements tab switching and color change operation explanation

When implementing this function, the method I bor...

What are the advantages of using B+Tree as an index in MySQL?

Table of contents Why do databases need indexes? ...