Skip to content

fix mixin issue(fix vuejs/vue-class-component#87) #5514

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 26, 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
9 changes: 6 additions & 3 deletions src/core/instance/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,27 @@ export function resolveConstructorOptions (Ctor: Class<Component>) {
function resolveModifiedOptions (Ctor: Class<Component>): ?Object {
let modified
const latest = Ctor.options
const extended = Ctor.extendOptions
const sealed = Ctor.sealedOptions
for (const key in latest) {
if (latest[key] !== sealed[key]) {
if (!modified) modified = {}
modified[key] = dedupe(latest[key], sealed[key])
modified[key] = dedupe(latest[key], extended[key], sealed[key])
}
}
return modified
}

function dedupe (latest, sealed) {
function dedupe (latest, extended, sealed) {
// compare latest and sealed to ensure lifecycle hooks won't be duplicated
// between merges
if (Array.isArray(latest)) {
const res = []
sealed = Array.isArray(sealed) ? sealed : [sealed]
extended = Array.isArray(extended) ? extended : [extended]
for (let i = 0; i < latest.length; i++) {
if (sealed.indexOf(latest[i]) < 0) {
// push original options and not sealed options to exclude duplicated options
if (extended.indexOf(latest[i]) >= 0 || sealed.indexOf(latest[i]) < 0) {
res.push(latest[i])
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/unit/features/global-api/mixin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,25 @@ describe('Global API: mixin', () => {
})
expect(spy).toHaveBeenCalledWith('hello')
})

// vue-class-component#87
it('should not drop original lifecycle hooks', () => {
const base = jasmine.createSpy('base')

const Base = Vue.extend({
beforeCreate: base
})

const injected = jasmine.createSpy('injected')

// inject a function
Base.options.beforeCreate = Base.options.beforeCreate.concat(injected)

Vue.mixin({})

new Base({})

expect(base).toHaveBeenCalled()
expect(injected).toHaveBeenCalled()
})
})