Let's talk about Vue's mixin and inheritance in detail

Let's talk about Vue's mixin and inheritance in detail

Preface

Mixin in Vue is a relatively simple knowledge point. Developers who are familiar with CSS preprocessing languages ​​such as less and sass must be familiar with mixin. The mixins in Vue are almost the same as the mixins in less, etc. They all inject some defined functions into the Vue components as is, which is somewhat similar to inheritance in object-oriented programming (I just said similar :)).

A mixin is an object whose properties can be the lifecycle hooks and properties of a Vue instance or component instance. When mixed in globally or into a component, the Vue instance or component instance will have the lifecycle hooks and properties defined in the mixin. If there are duplicates between the two, they will be merged according to certain rules.

Mixin

  • Extract the function codes that are reused in multiple vue files into a single js file and call it where it is needed.
  • Define an object in a js file. In the object, you can write data, methods, components and other codes that can be defined in <script> in the vue file.

Mixin Note (duplicate name)

  • If there is a duplication between the data variable name in the component and the data variable name in the mixin, the component takes precedence;
  • If there are duplicate names of methods, computed, and wath in a component and methods, computed, and wath in a mixin, the component takes precedence;
  • When the life hook function in the component and the life hook function in the mixin have the same name, both will be executed, but the hook function in the component will be executed first;

Local Mixin

Global Mixins

Definition and global registration

Call

inherit

  • Note: Multiple inheritance is not suitable here, as multiple inheritance problems will occur more frequently.
  • In addition to inheriting .vue files, extends can also use objects in js files like mixin.
  • The HTML in the template in the extends inheritance.vue file cannot be inherited

Difference between mixin and inheritance

  • Let's first look at the definition in the official document. In fact, both can be understood as inheritance;
  • Mixins receive an array of objects (which can be understood as multiple inheritance);
  • What extends receives is an object or a function (which can be understood as single inheritance).
  • Note: If a component uses both inheritance and mixins, if there is a duplicate name between the two, the mixin will override the inheritance.

Summarize

This is the end of this article about Vue mixins and inheritance. For more relevant Vue mixins and inheritance content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of inheritance and extension of Vue2.0 components
  • Web project development VUE mixing and inheritance principle
  • Detailed explanation of mixed inheritance in Vue

<<:  How to implement scheduled backup of MySQL in Linux

>>:  MySQL master-slave replication delay causes and solutions

Recommend

Build Maven projects faster in Docker

Table of contents I. Overview 2. Conventional mul...

MySQL query optimization using custom variables

Table of contents Optimizing sorting queries Avoi...

XHTML introductory tutorial: text formatting and special characters

<br />This section introduces how to impleme...

Detailed explanation of tcpdump command examples in Linux

Preface To put it simply, tcpdump is a packet ana...

Detailed explanation of the simple use of MySQL query cache

Table of contents 1. Implementation process of qu...

Example of customizing the style of the form file selection box

Copy code The code is as follows: <!DOCTYPE ht...

Tutorial diagram of installing centos7.3 on vmware virtual machine

VMware Preparation CentOS preparation, here is Ce...

Detailed explanation of samba + OPENldap to build a file sharing server

Here I use samba (file sharing service) v4.9.1 + ...

How to make if judgment in js as smooth as silk

Table of contents Preface Code Implementation Ide...

Detailed explanation of Tomcat's Server Options

1. Configuration By default, the first two are no...

Detailed explanation of common for loop in JavaScript statements

There are many loop statements in JavaScript, inc...

How to build a multi-node Elastic stack cluster on RHEL8 /CentOS8

Elastic stack, commonly known as ELK stack, is a ...

Detailed explanation of the basic use of Apache POI

Table of contents Basic Introduction Getting Star...

HTML page header code is completely clear

All the following codes are between <head>.....

How to install Oracle on Windows Server 2016

1. Install Oracle There are too many Oracle insta...