Detailed explanation of mixed inheritance in Vue

Detailed explanation of mixed inheritance in Vue

The effect of mixed inheritance:

A has a data attribute and a say method

B has a see method and a name attribute

After A inherits from B, in addition to its own data attribute and a say method, A also has B's see method and a name attribute

After mixing AB in C, C has its own stuff and a data attribute and a say method of A, and a see method and a name attribute of B.

1. Inheritance

Vue.extend Method

Vue.extend( Vue ComponentOptions ) is a global method that uses the base Vue constructor to create a "subclass". The argument is an object containing component options (Vue ComponentOptions).​

The data attribute is a special case and needs to be noted - it must be a function (factory function) in Vue.extend()

// Create a constructor ja
var Profile = Vue.extend({
  template: '<p>{{firstName}} {{lastName}} aka {{alias}}</p>',
  data: function () {
    return {
      firstName: 'Walter',
      lastName: 'White',
      alias: 'Heisenberg'
    }
  }
})
// Create an instance of the Profile class and attach it to an element.
new Profile().$mount('#mount-point')

​extends property

extends allows declaring extensions of another component (can be a simple options object or a constructor) without using Vue.extend. This is mainly to facilitate the extension of single-file components. Its type is Object or Function​

pageTwo.vue File

<template>
    <div>
        <h3>Page Two</h3>
        <paging :total="total" :pageSize="pageSize" :sizeOptions="sizeOptions"/>
    </div>
</template>
<script>
    import PagingData from '../component/PagingData.vue'
    export default {
        // Implement inheritance extends: PagingData
    }
</script>

Note: paging in the above file is a globally registered custom component. PagingData is also a component, but it is not registered. Instead, its content is merged into pageTwo through inheritance.

2. Mixins

A mixin object can contain arbitrary component options. When a component uses a mixin, all of the mixin's options will be "mixed" into the component's own options.

Merge rules

When a component and a mixin have options with the same name, those options will be "merged" in an appropriate manner.

1. Data objects are recursively merged internally, and component data takes precedence when conflicts occur.

2. Hook functions (lifecycle functions) with the same name will be merged into an array, so they will all be called. Additionally, the mixin's hooks will be called before the component's own hooks.

3. Options whose values ​​are objects, such as methods, components, and directives, will be merged into the same object. When the key names of two objects conflict, the key-value pair of the component object is taken.

Inheritance (extends) is also the rule of this merge.

You can use mixins to implement multiple inheritance. Mixins are not used to implement inheritance. Merge multiple Vue ComponentOptions (Vue optional component objects) together.

Format : mixins: [Merge component 1, Merge component 2, …]

pageOne.vue File

<template>
    <div>
        <h3>Page One</h3>
        <hr/>
        <paging @pageChangeEvt="cb" :total="total" :pageSize="pageSize" :sizeOptions="sizeOptions"/>
    </div>
</template>
<script>
    import PagingData from '../component/PagingData.vue'
    import PagingFunc from '../component/PagingFunc'
    export default {
        // extends: {PagingFunc, PagingData},
        // extends: [PagingFunc, PagingData],
        mixins: [PagingFunc, PagingData],
    }
</script>

Note: paging in the above file is a globally registered custom component. PagingData and PagingFunc are also components, but they are not registered. Instead, their contents are merged into pageOne through mixing.

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of inheritance and extension of Vue2.0 components
  • Web project development VUE mixing and inheritance principle
  • Let's talk about Vue's mixin and inheritance in detail

<<:  Web Design Tutorial (2): On Imitation and Plagiarism

>>:  10 HTML table-related tags

Recommend

CSS to achieve the sticky effect of two balls intersecting sample code

This is an effect created purely using CSS. To pu...

Example code for implementing WeChat account splitting with Nodejs

The company's business scenario requires the ...

How to install PostgreSQL11 on CentOS7

Install PostgreSQL 11 on CentOS 7 PostgreSQL: The...

Detailed summary of MySQL and connection-related timeouts

MySQL and connection related timeouts Preface: To...

Make your website automatically use IE7 compatibility mode when browsing IE8

Preface To help ensure that your web pages have a ...

CSS3 realizes particle animation effect when matching kings

When coding, you will find that many things have ...

Detailed explanation of Docker container data volumes

What is Let’s first look at the concept of Docker...

How to modify the default encoding of mysql in Linux

During the development process, if garbled charac...

Solve the MySQL login 1045 problem under centos

Since the entire application needs to be deployed...

Detailed installation process of nodejs management tool nvm

nvm nvm is responsible for managing multiple vers...

Getting Started Tutorial for Beginners ④: How to bind subdirectories

To understand what this means, we must first know ...

Let’s talk about the symbol data type in ES6 in detail

Table of contents Symbol Data Type The reason why...

3 functions of toString method in js

Table of contents 1. Three functions of toString ...

Detailed explanation of the usage of MySQL memory tables and temporary tables

Usage of MySQL memory tables and temporary tables...