From: hiroki Date: Mon, 30 Nov 2020 21:36:02 +0000 (-0500) Subject: fix(provide): support symbols in applyOptions (#2616) X-Git-Tag: v3.0.4~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7a1a782642a13d5fcc6b8c738a5ce8f8c657e1b5;p=thirdparty%2Fvuejs%2Fcore.git fix(provide): support symbols in applyOptions (#2616) fix #2615 --- diff --git a/packages/runtime-core/__tests__/apiOptions.spec.ts b/packages/runtime-core/__tests__/apiOptions.spec.ts index ab60ac7ca8..d60842db45 100644 --- a/packages/runtime-core/__tests__/apiOptions.spec.ts +++ b/packages/runtime-core/__tests__/apiOptions.spec.ts @@ -251,6 +251,7 @@ describe('api: options', () => { }) test('provide/inject', () => { + const symbolKey = Symbol() const Root = defineComponent({ data() { return { @@ -259,7 +260,8 @@ describe('api: options', () => { }, provide() { return { - a: this.a + a: this.a, + [symbolKey]: 2 } }, render() { @@ -271,7 +273,9 @@ describe('api: options', () => { h(ChildE), h(ChildF), h(ChildG), - h(ChildH) + h(ChildH), + h(ChildI), + h(ChildJ) ] } }) @@ -321,7 +325,15 @@ describe('api: options', () => { default: () => 5 } }) - expect(renderToString(h(Root))).toBe(`11112345`) + const ChildI = defineChild({ + b: symbolKey + }) + const ChildJ = defineChild({ + b: { + from: symbolKey + } + }) + expect(renderToString(h(Root))).toBe(`1111234522`) }) test('provide accessing data in extends', () => { diff --git a/packages/runtime-core/src/apiInject.ts b/packages/runtime-core/src/apiInject.ts index 186b411b24..402113cd1b 100644 --- a/packages/runtime-core/src/apiInject.ts +++ b/packages/runtime-core/src/apiInject.ts @@ -5,7 +5,7 @@ import { warn } from './warning' export interface InjectionKey extends Symbol {} -export function provide(key: InjectionKey | string, value: T) { +export function provide(key: InjectionKey | string | number, value: T) { if (!currentInstance) { if (__DEV__) { warn(`provide() can only be used inside setup().`) diff --git a/packages/runtime-core/src/componentOptions.ts b/packages/runtime-core/src/componentOptions.ts index 89bd8c0605..76e61a44ed 100644 --- a/packages/runtime-core/src/componentOptions.ts +++ b/packages/runtime-core/src/componentOptions.ts @@ -667,9 +667,9 @@ export function applyOptions( const provides = isFunction(provideOptions) ? provideOptions.call(publicThis) : provideOptions - for (const key in provides) { + Reflect.ownKeys(provides).forEach(key => { provide(key, provides[key]) - } + }) }) }