I want to use a global mixin in vue3. Using the mixin property - mixin:[mixinName] in createApp does not work. Using the method .mixin(mixinName) will work. What is the difference?
Not working:
return createApp({
mixins: [utilsMixin],
setup() {
return () => h(HelloWorld, {}, () => childComponents)
}
})
Working Version:
return createApp({
setup() {
return () => h(HelloWorld, {}, () => childComponents)
}
}).mixin(utilsMixin)
Both variants work. Check the playground below.
But you should be careful by mixing Options API with Composition API.
The
mixinsoptionmixins: [utilsMixin]is from Options APIand the
setup()function is from Composition API.Using
Options APItogether withComposition APIcould cause conflicts.I should also state that using Mixins is deprecated with Vue 3.
No Longer Recommended
In Vue 2, mixins were the primary mechanism for creating reusable chunks of component logic. While mixins continue to be supported in Vue 3, Composition API is now the preferred approach for code reuse between components.