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(provide): support symbols in applyOptions #2616

Merged
merged 1 commit into from
Nov 30, 2020
Merged
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
18 changes: 15 additions & 3 deletions packages/runtime-core/__tests__/apiOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ describe('api: options', () => {
})

test('provide/inject', () => {
const symbolKey = Symbol()
const Root = defineComponent({
data() {
return {
Expand All @@ -259,7 +260,8 @@ describe('api: options', () => {
},
provide() {
return {
a: this.a
a: this.a,
[symbolKey]: 2
}
},
render() {
Expand All @@ -271,7 +273,9 @@ describe('api: options', () => {
h(ChildE),
h(ChildF),
h(ChildG),
h(ChildH)
h(ChildH),
h(ChildI),
h(ChildJ)
]
}
})
Expand Down Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/apiInject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { warn } from './warning'

export interface InjectionKey<T> extends Symbol {}

export function provide<T>(key: InjectionKey<T> | string, value: T) {
export function provide<T>(key: InjectionKey<T> | string | number, value: T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@privatenumber @yyx990803 Why | number is added

Copy link
Contributor Author

@privatenumber privatenumber Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Back when this was written, Reflect.ownKeys() returned PropertyKey[], which includes the number type.

Now that Reflect.ownKeys() has been updated to return string | symbol, the number type can be removed.

if (!currentInstance) {
if (__DEV__) {
warn(`provide() can only be used inside setup().`)
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,9 +655,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])
}
})
})
}

Expand Down