Home > Article > Web Front-end > 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
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' }
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!