Skip to content

Commit 12b7122

Browse files
Yusuke Otsukayyx990803
Yusuke Otsuka
authored andcommitted
fix mixin issue (vuejs#5514)
1 parent 2a247fc commit 12b7122

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/core/instance/init.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,27 @@ export function resolveConstructorOptions (Ctor: Class<Component>) {
115115
function resolveModifiedOptions (Ctor: Class<Component>): ?Object {
116116
let modified
117117
const latest = Ctor.options
118+
const extended = Ctor.extendOptions
118119
const sealed = Ctor.sealedOptions
119120
for (const key in latest) {
120121
if (latest[key] !== sealed[key]) {
121122
if (!modified) modified = {}
122-
modified[key] = dedupe(latest[key], sealed[key])
123+
modified[key] = dedupe(latest[key], extended[key], sealed[key])
123124
}
124125
}
125126
return modified
126127
}
127128

128-
function dedupe (latest, sealed) {
129+
function dedupe (latest, extended, sealed) {
129130
// compare latest and sealed to ensure lifecycle hooks won't be duplicated
130131
// between merges
131132
if (Array.isArray(latest)) {
132133
const res = []
133134
sealed = Array.isArray(sealed) ? sealed : [sealed]
135+
extended = Array.isArray(extended) ? extended : [extended]
134136
for (let i = 0; i < latest.length; i++) {
135-
if (sealed.indexOf(latest[i]) < 0) {
137+
// push original options and not sealed options to exclude duplicated options
138+
if (extended.indexOf(latest[i]) >= 0 || sealed.indexOf(latest[i]) < 0) {
136139
res.push(latest[i])
137140
}
138141
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,25 @@ describe('Global API: mixin', () => {
141141
})
142142
expect(spy).toHaveBeenCalledWith('hello')
143143
})
144+
145+
// vue-class-component#87
146+
it('should not drop original lifecycle hooks', () => {
147+
const base = jasmine.createSpy('base')
148+
149+
const Base = Vue.extend({
150+
beforeCreate: base
151+
})
152+
153+
const injected = jasmine.createSpy('injected')
154+
155+
// inject a function
156+
Base.options.beforeCreate = Base.options.beforeCreate.concat(injected)
157+
158+
Vue.mixin({})
159+
160+
new Base({})
161+
162+
expect(base).toHaveBeenCalled()
163+
expect(injected).toHaveBeenCalled()
164+
})
144165
})

0 commit comments

Comments
 (0)