Vue 3 Mixins
Mixins define a set of reusable methods or computed properties. A mixin object can contain any component options. When a component uses a mixin object, all of the mixin object's options are merged into the component's own options.
Here is a simple example:
Example
//
Define a mixin object
const
myMixin
=
{
created
(
)
{
this
.
hello
(
)
}
,
methods
:
{
hello
(
)
{
console
.
log
(
'
Welcome to the mixin example - RUNOOB!
'
)
}
}
}
//
Define an application using the mixin
const
app
=
Vue
.
createApp
(
{
mixins
:
[
myMixin
]
}
)
app
.
mount
(
'
#app
'
)
//
=> "Welcome to the mixin example - RUNOOB!"
Try it »
Option Merging
When a component and a mixin object contain options with the same name, these options are merged in an appropriate way.
For example, data objects are shallow-merged internally (one level of property depth), and when conflicting with the component's data, the component data takes precedence.
In the example below, the Vue instance and the mixin object contain the same methods. From the output, you can see that the two options are merged.
Example
DOCTYPE
html
<html> <head> <meta charset="utf-8"> <title Vue>Test example - Runoob
(
runoob
.
com
)
title
<script src="https://unpkg.com/vue@next" script head> <body> <div id="app" div> <script type="text/javascript" const myMixin={data(){return{message>:
'
hello
'
,
foo
:
'
runoob
'
}
}
}
const
app
=
Vue
.
createApp
(
{
mixins
:
[
myMixin
]
,
data
(
)
{
return
{
message
:
'
goodbye
'
,
bar
:
'
def
'
}
}
,
created
(
)
{
document
.
write
(
JSON
.
stringify
(
this
.$
data
)
)
}
}
)
The output is:
{"message":"goodbye","foo":"runoob","bar":"def"}Try it »
Hook functions with the same name are merged into an array, so all are called. In addition, the mixin object's hooks are called before the component's own hooks.
const myMixin = { created() { console.log('mixin 对象的钩子被调用') } } const app = Vue.createApp({ mixins: [myMixin], created() { console.log('组件钩子被调用') } }) // => "mixin object hook is called" // => "Component hook called"Options whose values are objects, such as methods, components, and directives, are merged into the same object. When two objects have conflicting keys, the component object's key-value pair is taken.
Example
const
myMixin
=
{
methods
:
{
foo
(
)
{
console
.
log
(
'
foo
'
)
}
,
conflicting
(
)
{
console
.
log
(
'
from mixin
'
)
}
}
}
const
app
=
Vue
.
createApp
(
{
mixins
:
[
myMixin
]
,
methods
:
{
bar
(
)
{
console
.
log
(
'
bar
'
)
}
,
conflicting
(
)
{
console
.
log
(
'
from self
'
)
}
}
}
)
const
vm
=
app
.
mount
(
'
#app
'
)
vm
.
foo
(
)
//
=> "foo"
vm
.
bar
(
)
//
=> "bar"
vm
.
conflicting
(
)
//
=> "from self"
In the example above, we called the following three methods:
vm.foo(); vm.bar(); vm.conflicting();From the output, if the methods option encounters the same function name, the Vue instance has higher priority and its output is executed.
Global Mixin
You can also register mixin objects globally. Use with caution! Once a global mixin object is used, it will affect ALL Vue instances created afterward. Used appropriately, it can inject processing logic into custom objects.
Example
const
app
=
Vue
.
createApp
(
{
myOption
:
'
hello!
'
}
)
//
Inject a handler for the custom option 'myOption'.
app
.
mixin
(
{
created
(
)
{
const
myOption
=
this
.$
options
.
myOption
if
(
myOption
)
{
document
.
write
(
myOption
)
}
}
}
)
app
.
mount
(
'
#app
'
)
//
=> "hello!"
Try it »
Use global mixin objects with caution, as they affect every individually created Vue instance (including third-party templates).