Detailed introduction to the usage of mixins and extends in 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') } }
Console output
extends created mixin1 created mixin2 created created
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' }
- 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.
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/instanceextends
Calling method: extends: CompAIt 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 AngularHow to implement validation in AngularAbout how to merge Object values when using JavaScriptAbout 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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.

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.

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.

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.

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.

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.

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.
