In Vue3, Mixins are a technique for sharing component options among multiple components. Through the mixing mechanism, we can inject some reusable code and functions into multiple components, so as to achieve code reuse and logic sharing. This article will introduce the concept and usage of mixin in Vue3 in detail to help you better understand and apply mixin.
What is a mixin?
A mixin is a technique for merging a set of component options into a target component. In Vue3, we can extend component options by defining a mixin object, and use the option to introduce the mixin object in the target component mixins
. When a component uses a mixin, it inherits all the properties and methods of the mixin.
define mixin object
To define a mixin object, we can create a plain JavaScript object and add the component options that need to be shared to this object. For example, let's create a loggerMixin
mixin called , which logs to the console:
const loggerMixin = {
created() {
console.log('Component created')
},
methods: {
log(message) {
console.log(message)
}
}
}
In the above code, we define an loggerMixin
object, which contains a created
hook function and a log
method. In created
the hook function, we output a log to indicate that the component has been created. The method log
is used to output custom log information.
import mixin object
In Vue3, we can use mixins
options to introduce mixins into components. By adding the mixin object to mixins
the options array, we can inherit all the properties and methods of the mixin object in the component.
import { defineComponent } from 'vue'
const ComponentA = defineComponent({
mixins: [loggerMixin],
created() {
this.log('Hello, Vue3')
}
})
In the above code, we defineComponent
created a ComponentA
component called with a function and mixins
introduced loggerMixin
the mixin object in options. In this way, ComponentA
the component inherits the loggerMixin
hook created
functions and log
methods.
When ComponentA
the component is created, created
the hook function will be called and output "Component created" to the console. At the same time, in created
the hook function, we call log
the method and pass a parameter "Hello, Vue3", which will output the log information in the console.
Resolving mixin naming conflicts
When using mixins, you may encounter mixin naming conflicts. When the mixin and the component itself have the same options, the component options override the mixin's options. For example, if both the component and the mixin have created
hooks, the component's own created
hooks will override the mixin's created
hooks.
To avoid mixin naming conflicts, we can use the options in the mixin object $options
to access the component itself. In this way, we can extend the component's options without being overridden by the component's options. For example, we can use it in the mixin object this.$options.created
to call the hook function of the component itself created
.
const loggerMixin = {
created() {
console.log('Mixin created')
},
methods: {
log(message) {
console.log(message)
}
}
}
const ComponentB = defineComponent({
mixins: [loggerMixin],
created() {
this.$options.created.call(this) // 调用组件自身的created钩子函数
this.log('Hello, Vue3')
}
})
In the above code, we define a loggerMixin
mixin object called , which contains a created
hook function and a log
method. In created
the hook function, we output a log indicating that the mixin object was created.
In ComponentB
the component, we introduce loggerMixin
the mixed-in object, created
call the component's own created
hook function in the hook function, and use log
the method to output log information.
order of mixing in
In Vue3, the merging order of mixed-in objects starts from the last element of the mixed-in array, and merges forward one by one. This means that if multiple mixins have the same options, the options of the later mixins will override the options of the earlier mixins.
For example, we have three mixins, namely mixinA
, mixinB
and mixinC
:
const mixinA = {
created() {
console.log('Mixin A created')
}
}
const mixinB = {
created() {
console.log('Mixin B created')
}
}
const mixinC = {
created() {
console.log('Mixin C created')
}
}
Now, we create a component and mixins
include these three mixins in the options:
const ComponentC = defineComponent({
mixins: [mixinA, mixinB, mixinC],
created() {
console.log('Component created')
}
})
In the above code, ComponentC
the component introduces mixinA
, , mixinB
and mixinC
three mixins. When ComponentC
the component is created, the console will output "Mixin A created", "Mixin B created", "Mixin C created" and "Component created" in sequence.
This illustrates the order in which mixins are merged, ie from back to front.
Summarize
In this article, we detailed the concept of Mixins in Vue3 and their usage. Through the mixing mechanism, we can inject some reusable code and functions into multiple components to achieve code reuse and logic sharing.
We learned how to define a mixin and how to include a mixin in a component. At the same time, we discussed the method of resolving mixin naming conflicts and the order of mixins.