Skip to content

Commit 6c3e6dc

Browse files
committed
support ES2015 Symbol in provide/inject
1 parent 135f06d commit 6c3e6dc

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/core/instance/inject.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/* @flow */
22

3+
import { isNative } from 'core/util/env'
4+
5+
const hasReflect = typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys)
6+
37
export function initInjections (vm: Component) {
48
const provide = vm.$options.provide
59
const inject: any = vm.$options.inject
@@ -12,7 +16,12 @@ export function initInjections (vm: Component) {
1216
// inject is :any because flow is not smart enough to figure out cached
1317
// isArray here
1418
const isArray = Array.isArray(inject)
15-
const keys = isArray ? inject : Object.keys(inject)
19+
const keys = isArray
20+
? inject
21+
: hasReflect
22+
? Reflect.ownKeys(inject)
23+
: Object.keys(inject)
24+
1625
for (let i = 0; i < keys.length; i++) {
1726
const key = keys[i]
1827
const provideKey = isArray ? key : inject[key]

src/core/util/env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const isServerRendering = () => {
3737
export const devtools = inBrowser && window.__VUE_DEVTOOLS_GLOBAL_HOOK__
3838

3939
/* istanbul ignore next */
40-
function isNative (Ctor: Function): boolean {
40+
export function isNative (Ctor: Function): boolean {
4141
return /native code/.test(Ctor.toString())
4242
}
4343

test/unit/features/options/inject.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue from 'vue'
2+
import { isNative } from 'core/util/env'
23

34
describe('Options provide/inject', () => {
45
let injected
@@ -124,4 +125,23 @@ describe('Options provide/inject', () => {
124125

125126
expect(vm.foo).toBe(1)
126127
})
128+
129+
if (typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys)) {
130+
it('with Symbol keys', () => {
131+
const s = Symbol()
132+
const vm = new Vue({
133+
template: `<child/>`,
134+
provide: {
135+
[s]: 123
136+
},
137+
components: {
138+
child: {
139+
inject: { s },
140+
template: `<div>{{ s }}</div>`
141+
}
142+
}
143+
}).$mount()
144+
expect(vm.$el.textContent).toBe('123')
145+
})
146+
}
127147
})

0 commit comments

Comments
 (0)