Skip to content

Commit a49c314

Browse files
committed
add details of object merging to mixins page
1 parent 12e10f0 commit a49c314

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/v2/guide/mixins.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,36 @@ var component = new Component() // => "hello from mixin!"
3333

3434
## Option Merging
3535

36-
When a mixin and the component itself contain overlapping options, they will be "merged" using appropriate strategies. For example, hook functions with the same name are merged into an array so that all of them will be called. In addition, mixin hooks will be called **before** the component's own hooks:
36+
When a mixin and the component itself contain overlapping options, they will be "merged" using appropriate strategies.
37+
38+
For example, data objects undergo a shallow merge (one property deep), with the component's data taking priority in cases of conflicts.
39+
40+
``` js
41+
var mixin = {
42+
data: function () {
43+
return {
44+
message: 'hello',
45+
foo: 'abc'
46+
}
47+
}
48+
}
49+
50+
new Vue({
51+
mixins: [mixin],
52+
data: function () {
53+
return {
54+
message: 'goodbye',
55+
bar: 'def'
56+
}
57+
},
58+
created: function () {
59+
console.log(this.$data)
60+
// => { message: "goodbye", foo: "abc", bar: "def" }
61+
}
62+
})
63+
```
64+
65+
Hook functions with the same name are merged into an array so that all of them will be called. Mixin hooks will be called **before** the component's own hooks.
3766

3867
``` js
3968
var mixin = {

0 commit comments

Comments
 (0)