Home Web Front-end JS Tutorial Detailed introduction to the usage of mixins and extends in vue

Detailed introduction to the usage of mixins and extends in vue

Jun 19, 2018 pm 04:24 PM
extends mixins vue

vue provides mixins and extends configuration items, which have been found to be very useful in recent use. Next, I will introduce to you the clever usage of mixins and extends in Vue through this article. Friends who need it can refer to it.

Vue provides mixins and extends configuration items, which I found very useful in recent use.

Mixing mixins and inheritance extends

Look at what the official documentation says. In fact, both can be understood as inheritance, and mixins receive object arrays (can be understood as multiple inheritance) ), extends receives an object or function (can be understood as single inheritance).

Inherit hook function

const extend = {
 created () {
  console.log('extends created')
 }
}
const mixin1 = {
 created () {
  console.log('mixin1 created')
 }
}
const mixin2 = {
 created () {
  console.log('mixin2 created')
 }
}
export default {
 extends: extend,
 mixins: [mixin1, mixin2],
 name: 'app',
 created () {
  console.log('created')
 }
}
Copy after login

Console output

extends created
mixin1 created
mixin2 created
created
Copy after login
  • Conclusion: Give priority to calling the parent class inherited by mixins and extends. The priority triggered by extends is higher, relative to the queue

  • ##push(extend, mixin1, minxin2, its own hook function)

  • After testing , the value inheritance rules of watch are the same.

Inherit methods

const extend = {
 data () {
  return {
   name: 'extend name'
  }
 }
}
const mixin1 = {
 data () {
  return {
   name: 'mixin1 name'
  }
 }
}
const mixin2 = {
 data () {
  return {
   name: 'mixin2 name'
  }
 }
}
// name = 'name'
export default {
 mixins: [mixin1, mixin2],
 extends: extend,
 name: 'app',
 data () {
  return {
   name: 'name'
  }
 }
}
// 只写出子类,name = 'mixin2 name',extends优先级高会被mixins覆盖
export default {
 mixins: [mixin1, mixin2],
 extends: extend,
 name: 'app'
}
// 只写出子类,name = 'mixin1 name',mixins后面继承会覆盖前面的
export default {
 mixins: [mixin2, mixin1],
 extends: extend,
 name: 'app'
}
Copy after login
  • Conclusion: If the subclass is declared again, the variables in data will be rewritten to The subcategory shall prevail.

  • If the subclass is not declared, the variables in data will be based on the last inherited parent class.

  • After testing, the inheritance rules for attributes in props, methods in methods and computed values ​​are the same.

The following is a separate introduction to mixins, extends, and extend

mixins

Calling method: mixins : [mixin1, mixin2]

is an expansion of the parent component, including methods, components, directives, etc. . .

When the hook function is triggered, the function of mixins is called first, and then the function of the parent component is called.

Although you can also add data and template attributes when creating a mixin, when the parent component also has this attribute, the parent will prevail. From this point, you can also see the producer's intention (expansion).

Objects in key-value format such as functions within data, methods, components and directives are all based on the parent component/instance

extends

Calling method: extends: CompA

It is also an extension of the parent component, similar to mixins, but its priority is inferior to the parent component

##extend Constructor of extended component

Automatically called when we call vue.component('a', {...})

Worth Note that the data in extend is a function

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to implement table sorting in Angular

How to implement validation in Angular

About how to merge Object values ​​when using JavaScript

About the use of pseudo arrays in JavaScript (detailed tutorial)

The above is the detailed content of Detailed introduction to the usage of mixins and extends in vue. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

How to use watch in vue How to use watch in vue Apr 07, 2025 pm 11:36 PM

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

What does vue multi-page development mean? What does vue multi-page development mean? Apr 07, 2025 pm 11:57 PM

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

How to reference js file with vue.js How to reference js file with vue.js Apr 07, 2025 pm 11:27 PM

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

How to return to previous page by vue How to return to previous page by vue Apr 07, 2025 pm 11:30 PM

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

How to use vue traversal How to use vue traversal Apr 07, 2025 pm 11:48 PM

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values ​​for each element; and the .map method can convert array elements into new arrays.

How to jump to the div of vue How to jump to the div of vue Apr 08, 2025 am 09:18 AM

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.

See all articles