Skip to content

Commit bd4819e

Browse files
Guillaume Chauyyx990803
Guillaume Chau
authored andcommitted
fix: data() should be called with vm as first argument in mixins
fix #7191
1 parent 6eccd8e commit bd4819e

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/core/util/options.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,18 @@ export function mergeDataOrFn (
8585
// it has to be a function to pass previous merges.
8686
return function mergedDataFn () {
8787
return mergeData(
88-
typeof childVal === 'function' ? childVal.call(this) : childVal,
89-
typeof parentVal === 'function' ? parentVal.call(this) : parentVal
88+
typeof childVal === 'function' ? childVal.call(this, this) : childVal,
89+
typeof parentVal === 'function' ? parentVal.call(this, this) : parentVal
9090
)
9191
}
9292
} else {
9393
return function mergedInstanceDataFn () {
9494
// instance merge
9595
const instanceData = typeof childVal === 'function'
96-
? childVal.call(vm)
96+
? childVal.call(vm, vm)
9797
: childVal
9898
const defaultData = typeof parentVal === 'function'
99-
? parentVal.call(vm)
99+
? parentVal.call(vm, vm)
100100
: parentVal
101101
if (instanceData) {
102102
return mergeData(instanceData, defaultData)

test/unit/features/options/data.spec.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe('Options data', () => {
107107
expect(vm.a).toBe(1)
108108
})
109109

110-
it('should called with this', () => {
110+
it('should be called with this', () => {
111111
const vm = new Vue({
112112
template: '<div><child></child></div>',
113113
provide: { foo: 1 },
@@ -123,4 +123,32 @@ describe('Options data', () => {
123123
}).$mount()
124124
expect(vm.$el.innerHTML).toBe('<span>foo:1</span>')
125125
})
126+
127+
it('should be called with vm as first argument when merged', () => {
128+
const superComponent = {
129+
data: ({ foo }) => ({ ext: 'ext:' + foo })
130+
}
131+
const mixins = [
132+
{
133+
data: ({ foo }) => ({ mixin1: 'm1:' + foo })
134+
},
135+
{
136+
data: ({ foo }) => ({ mixin2: 'm2:' + foo })
137+
}
138+
]
139+
const vm = new Vue({
140+
template: '<div><child></child></div>',
141+
provide: { foo: 1 },
142+
components: {
143+
child: {
144+
extends: superComponent,
145+
mixins,
146+
template: '<span>{{bar}}-{{ext}}-{{mixin1}}-{{mixin2}}</span>',
147+
inject: ['foo'],
148+
data: ({ foo }) => ({ bar: 'foo:' + foo })
149+
}
150+
}
151+
}).$mount()
152+
expect(vm.$el.innerHTML).toBe('<span>foo:1-ext:1-m1:1-m2:1</span>')
153+
})
126154
})

0 commit comments

Comments
 (0)