Skip to content

Commit 086ae44

Browse files
ktsnyyx990803
authored andcommitted
allow an extended constructor as global mixin (vuejs#5448)
1 parent 5eeb9de commit 086ae44

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/core/util/options.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* @flow */
22

3-
import Vue from '../instance/index'
43
import config from '../config'
54
import { warn } from './debug'
65
import { set } from '../observer/index'
@@ -275,21 +274,20 @@ export function mergeOptions (
275274
if (process.env.NODE_ENV !== 'production') {
276275
checkComponents(child)
277276
}
277+
278+
if (typeof child === 'function') {
279+
child = child.options
280+
}
281+
278282
normalizeProps(child)
279283
normalizeDirectives(child)
280284
const extendsFrom = child.extends
281285
if (extendsFrom) {
282-
parent = typeof extendsFrom === 'function'
283-
? mergeOptions(parent, extendsFrom.options, vm)
284-
: mergeOptions(parent, extendsFrom, vm)
286+
parent = mergeOptions(parent, extendsFrom, vm)
285287
}
286288
if (child.mixins) {
287289
for (let i = 0, l = child.mixins.length; i < l; i++) {
288-
let mixin = child.mixins[i]
289-
if (mixin.prototype instanceof Vue) {
290-
mixin = mixin.options
291-
}
292-
parent = mergeOptions(parent, mixin, vm)
290+
parent = mergeOptions(parent, child.mixins[i], vm)
293291
}
294292
}
295293
const options = {}

test/unit/features/global-api/mixin.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,21 @@ describe('Global API: mixin', () => {
124124
expect(Test.options.computed.$style()).toBe(123)
125125
expect(Test.options.beforeCreate).toEqual([mixinSpy, baseSpy, spy])
126126
})
127+
128+
// vue-class-component#83
129+
it('should work for a constructor mixin', () => {
130+
const spy = jasmine.createSpy('global mixin')
131+
const Mixin = Vue.extend({
132+
created () {
133+
spy(this.$options.myOption)
134+
}
135+
})
136+
137+
Vue.mixin(Mixin)
138+
139+
new Vue({
140+
myOption: 'hello'
141+
})
142+
expect(spy).toHaveBeenCalledWith('hello')
143+
})
127144
})

0 commit comments

Comments
 (0)