Skip to content

Commit f06518a

Browse files
committed
fix(runtime-core): fix provide function data access in extends/mixins
fix #2300
1 parent cbcec6e commit f06518a

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

packages/runtime-core/__tests__/apiOptions.spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,36 @@ describe('api: options', () => {
324324
expect(renderToString(h(Root))).toBe(`11112345`)
325325
})
326326

327+
test('provide accessing data in extends', () => {
328+
const Base = defineComponent({
329+
data() {
330+
return {
331+
a: 1
332+
}
333+
},
334+
provide() {
335+
return {
336+
a: this.a
337+
}
338+
}
339+
})
340+
341+
const Child = {
342+
inject: ['a'],
343+
render() {
344+
return (this as any).a
345+
}
346+
}
347+
348+
const Root = defineComponent({
349+
extends: Base,
350+
render() {
351+
return h(Child)
352+
}
353+
})
354+
expect(renderToString(h(Root))).toBe(`1`)
355+
})
356+
327357
test('lifecycle', async () => {
328358
const count = ref(0)
329359
const root = nodeOps.createElement('div')

packages/runtime-core/src/componentOptions.ts

+38-11
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ export function applyOptions(
430430
options: ComponentOptions,
431431
deferredData: DataFn[] = [],
432432
deferredWatch: ComponentWatchOptions[] = [],
433+
deferredProvide: (Data | Function)[] = [],
433434
asMixin: boolean = false
434435
) {
435436
const {
@@ -483,16 +484,29 @@ export function applyOptions(
483484
)
484485
isInBeforeCreate = false
485486
// global mixins are applied first
486-
applyMixins(instance, globalMixins, deferredData, deferredWatch)
487+
applyMixins(
488+
instance,
489+
globalMixins,
490+
deferredData,
491+
deferredWatch,
492+
deferredProvide
493+
)
487494
}
488495

489496
// extending a base component...
490497
if (extendsOptions) {
491-
applyOptions(instance, extendsOptions, deferredData, deferredWatch, true)
498+
applyOptions(
499+
instance,
500+
extendsOptions,
501+
deferredData,
502+
deferredWatch,
503+
deferredProvide,
504+
true
505+
)
492506
}
493507
// local mixins
494508
if (mixins) {
495-
applyMixins(instance, mixins, deferredData, deferredWatch)
509+
applyMixins(instance, mixins, deferredData, deferredWatch, deferredProvide)
496510
}
497511

498512
const checkDuplicateProperties = __DEV__ ? createDuplicateChecker() : null
@@ -634,12 +648,17 @@ export function applyOptions(
634648
}
635649

636650
if (provideOptions) {
637-
const provides = isFunction(provideOptions)
638-
? provideOptions.call(publicThis)
639-
: provideOptions
640-
for (const key in provides) {
641-
provide(key, provides[key])
642-
}
651+
deferredProvide.push(provideOptions)
652+
}
653+
if (!asMixin && deferredProvide.length) {
654+
deferredProvide.forEach(provideOptions => {
655+
const provides = isFunction(provideOptions)
656+
? provideOptions.call(publicThis)
657+
: provideOptions
658+
for (const key in provides) {
659+
provide(key, provides[key])
660+
}
661+
})
643662
}
644663

645664
// asset options.
@@ -777,10 +796,18 @@ function applyMixins(
777796
instance: ComponentInternalInstance,
778797
mixins: ComponentOptions[],
779798
deferredData: DataFn[],
780-
deferredWatch: ComponentWatchOptions[]
799+
deferredWatch: ComponentWatchOptions[],
800+
deferredProvide: (Data | Function)[]
781801
) {
782802
for (let i = 0; i < mixins.length; i++) {
783-
applyOptions(instance, mixins[i], deferredData, deferredWatch, true)
803+
applyOptions(
804+
instance,
805+
mixins[i],
806+
deferredData,
807+
deferredWatch,
808+
deferredProvide,
809+
true
810+
)
784811
}
785812
}
786813

0 commit comments

Comments
 (0)