fix(runtime-core): fix provide function data access in extends/mixins
fix #2300
This commit is contained in:
parent
cbcec6e978
commit
f06518a8c9
@ -324,6 +324,36 @@ describe('api: options', () => {
|
|||||||
expect(renderToString(h(Root))).toBe(`11112345`)
|
expect(renderToString(h(Root))).toBe(`11112345`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('provide accessing data in extends', () => {
|
||||||
|
const Base = defineComponent({
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
a: 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
provide() {
|
||||||
|
return {
|
||||||
|
a: this.a
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const Child = {
|
||||||
|
inject: ['a'],
|
||||||
|
render() {
|
||||||
|
return (this as any).a
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const Root = defineComponent({
|
||||||
|
extends: Base,
|
||||||
|
render() {
|
||||||
|
return h(Child)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
expect(renderToString(h(Root))).toBe(`1`)
|
||||||
|
})
|
||||||
|
|
||||||
test('lifecycle', async () => {
|
test('lifecycle', async () => {
|
||||||
const count = ref(0)
|
const count = ref(0)
|
||||||
const root = nodeOps.createElement('div')
|
const root = nodeOps.createElement('div')
|
||||||
|
@ -430,6 +430,7 @@ export function applyOptions(
|
|||||||
options: ComponentOptions,
|
options: ComponentOptions,
|
||||||
deferredData: DataFn[] = [],
|
deferredData: DataFn[] = [],
|
||||||
deferredWatch: ComponentWatchOptions[] = [],
|
deferredWatch: ComponentWatchOptions[] = [],
|
||||||
|
deferredProvide: (Data | Function)[] = [],
|
||||||
asMixin: boolean = false
|
asMixin: boolean = false
|
||||||
) {
|
) {
|
||||||
const {
|
const {
|
||||||
@ -483,16 +484,29 @@ export function applyOptions(
|
|||||||
)
|
)
|
||||||
isInBeforeCreate = false
|
isInBeforeCreate = false
|
||||||
// global mixins are applied first
|
// global mixins are applied first
|
||||||
applyMixins(instance, globalMixins, deferredData, deferredWatch)
|
applyMixins(
|
||||||
|
instance,
|
||||||
|
globalMixins,
|
||||||
|
deferredData,
|
||||||
|
deferredWatch,
|
||||||
|
deferredProvide
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// extending a base component...
|
// extending a base component...
|
||||||
if (extendsOptions) {
|
if (extendsOptions) {
|
||||||
applyOptions(instance, extendsOptions, deferredData, deferredWatch, true)
|
applyOptions(
|
||||||
|
instance,
|
||||||
|
extendsOptions,
|
||||||
|
deferredData,
|
||||||
|
deferredWatch,
|
||||||
|
deferredProvide,
|
||||||
|
true
|
||||||
|
)
|
||||||
}
|
}
|
||||||
// local mixins
|
// local mixins
|
||||||
if (mixins) {
|
if (mixins) {
|
||||||
applyMixins(instance, mixins, deferredData, deferredWatch)
|
applyMixins(instance, mixins, deferredData, deferredWatch, deferredProvide)
|
||||||
}
|
}
|
||||||
|
|
||||||
const checkDuplicateProperties = __DEV__ ? createDuplicateChecker() : null
|
const checkDuplicateProperties = __DEV__ ? createDuplicateChecker() : null
|
||||||
@ -634,12 +648,17 @@ export function applyOptions(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (provideOptions) {
|
if (provideOptions) {
|
||||||
|
deferredProvide.push(provideOptions)
|
||||||
|
}
|
||||||
|
if (!asMixin && deferredProvide.length) {
|
||||||
|
deferredProvide.forEach(provideOptions => {
|
||||||
const provides = isFunction(provideOptions)
|
const provides = isFunction(provideOptions)
|
||||||
? provideOptions.call(publicThis)
|
? provideOptions.call(publicThis)
|
||||||
: provideOptions
|
: provideOptions
|
||||||
for (const key in provides) {
|
for (const key in provides) {
|
||||||
provide(key, provides[key])
|
provide(key, provides[key])
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// asset options.
|
// asset options.
|
||||||
@ -777,10 +796,18 @@ function applyMixins(
|
|||||||
instance: ComponentInternalInstance,
|
instance: ComponentInternalInstance,
|
||||||
mixins: ComponentOptions[],
|
mixins: ComponentOptions[],
|
||||||
deferredData: DataFn[],
|
deferredData: DataFn[],
|
||||||
deferredWatch: ComponentWatchOptions[]
|
deferredWatch: ComponentWatchOptions[],
|
||||||
|
deferredProvide: (Data | Function)[]
|
||||||
) {
|
) {
|
||||||
for (let i = 0; i < mixins.length; i++) {
|
for (let i = 0; i < mixins.length; i++) {
|
||||||
applyOptions(instance, mixins[i], deferredData, deferredWatch, true)
|
applyOptions(
|
||||||
|
instance,
|
||||||
|
mixins[i],
|
||||||
|
deferredData,
|
||||||
|
deferredWatch,
|
||||||
|
deferredProvide,
|
||||||
|
true
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user