Detailed introduction to the usage of mixins and extends in vue

亚连
Release: 2018-06-19 16:24:06
Original
1869 people have browsed it

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

##extendConstructor 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!

Related labels:
source:php.cn
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