Skip to content
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

fix(runtime-core): call hooks when using mixins with extends #3416

Closed
Closed
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
82 changes: 82 additions & 0 deletions packages/runtime-core/__tests__/apiOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,88 @@ describe('api: options', () => {
])
})

test('extends in mixins', () => {
const calls: string[] = []
const BaseA = {
beforeCreate() {
calls.push('beforeCreateA')
},
created() {
calls.push('createdA')
}
}
const MixinB = {
mixins: [BaseA],
beforeCreate() {
calls.push('beforeCreateB')
},
created() {
calls.push('createdB')
}
}
const Comp = {
extends: MixinB,
beforeCreate() {
calls.push('selfBeforeCreate')
},
created() {
calls.push('selfCreated')
},
render() {}
}

renderToString(h(Comp))
expect(calls).toEqual([
'beforeCreateA',
'beforeCreateB',
'selfBeforeCreate',
'createdA',
'createdB',
'selfCreated'
])
})

test('mixins in extends', () => {
const calls: string[] = []
const BaseA = {
beforeCreate() {
calls.push('beforeCreateA')
},
created() {
calls.push('createdA')
}
}
const ExtendB = {
extends: BaseA,
beforeCreate() {
calls.push('beforeCreateB')
},
created() {
calls.push('createdB')
}
}
const Comp = {
mixins: [ExtendB],
beforeCreate() {
calls.push('selfBeforeCreate')
},
created() {
calls.push('selfCreated')
},
render() {}
}

renderToString(h(Comp))
expect(calls).toEqual([
'beforeCreateA',
'beforeCreateB',
'selfBeforeCreate',
'createdA',
'createdB',
'selfCreated'
])
})

test('flatten merged options', async () => {
const MixinBase = {
msg1: 'base'
Expand Down
9 changes: 8 additions & 1 deletion packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,12 +839,15 @@ function callHookFromExtends(
if (base.extends) {
callHookFromExtends(name, type, base.extends, instance)
}
if (base.mixins) {
callHookFromMixins(name, type, base.mixins, instance)
}

const baseHook = base[name]
if (baseHook) {
callWithAsyncErrorHandling(baseHook.bind(instance.proxy!), instance, type)
}
}

function callHookFromMixins(
name: 'beforeCreate' | 'created',
type: LifecycleHooks,
Expand All @@ -856,6 +859,10 @@ function callHookFromMixins(
if (chainedMixins) {
callHookFromMixins(name, type, chainedMixins, instance)
}
const chainedExtends = mixins[i].extends
if (chainedExtends) {
callHookFromExtends(name, type, chainedExtends, instance)
}
const fn = mixins[i][name]
if (fn) {
callWithAsyncErrorHandling(fn.bind(instance.proxy!), instance, type)
Expand Down