Skip to content

allow an extended constructor as global mixin (fix vue-class-component#83) #5448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/core/util/options.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* @flow */

import Vue from '../instance/index'
import config from '../config'
import { warn } from './debug'
import { set } from '../observer/index'
Expand Down Expand Up @@ -275,21 +274,20 @@ export function mergeOptions (
if (process.env.NODE_ENV !== 'production') {
checkComponents(child)
}

if (typeof child === 'function') {
child = child.options
}

normalizeProps(child)
normalizeDirectives(child)
const extendsFrom = child.extends
if (extendsFrom) {
parent = typeof extendsFrom === 'function'
? mergeOptions(parent, extendsFrom.options, vm)
: mergeOptions(parent, extendsFrom, vm)
parent = mergeOptions(parent, extendsFrom, vm)
}
if (child.mixins) {
for (let i = 0, l = child.mixins.length; i < l; i++) {
let mixin = child.mixins[i]
if (mixin.prototype instanceof Vue) {
mixin = mixin.options
}
parent = mergeOptions(parent, mixin, vm)
parent = mergeOptions(parent, child.mixins[i], vm)
}
}
const options = {}
Expand Down
17 changes: 17 additions & 0 deletions test/unit/features/global-api/mixin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,21 @@ describe('Global API: mixin', () => {
expect(Test.options.computed.$style()).toBe(123)
expect(Test.options.beforeCreate).toEqual([mixinSpy, baseSpy, spy])
})

// vue-class-component#83
it('should work for a constructor mixin', () => {
const spy = jasmine.createSpy('global mixin')
const Mixin = Vue.extend({
created () {
spy(this.$options.myOption)
}
})

Vue.mixin(Mixin)

new Vue({
myOption: 'hello'
})
expect(spy).toHaveBeenCalledWith('hello')
})
})