support ES2015 Symbol in provide/inject

This commit is contained in:
Evan You 2017-02-25 17:13:41 -05:00
parent 135f06d25f
commit 6c3e6dcdce
3 changed files with 31 additions and 2 deletions

View File

@ -1,5 +1,9 @@
/* @flow */
import { isNative } from 'core/util/env'
const hasReflect = typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys)
export function initInjections (vm: Component) {
const provide = vm.$options.provide
const inject: any = vm.$options.inject
@ -12,7 +16,12 @@ export function initInjections (vm: Component) {
// inject is :any because flow is not smart enough to figure out cached
// isArray here
const isArray = Array.isArray(inject)
const keys = isArray ? inject : Object.keys(inject)
const keys = isArray
? inject
: hasReflect
? Reflect.ownKeys(inject)
: Object.keys(inject)
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
const provideKey = isArray ? key : inject[key]

View File

@ -37,7 +37,7 @@ export const isServerRendering = () => {
export const devtools = inBrowser && window.__VUE_DEVTOOLS_GLOBAL_HOOK__
/* istanbul ignore next */
function isNative (Ctor: Function): boolean {
export function isNative (Ctor: Function): boolean {
return /native code/.test(Ctor.toString())
}

View File

@ -1,4 +1,5 @@
import Vue from 'vue'
import { isNative } from 'core/util/env'
describe('Options provide/inject', () => {
let injected
@ -124,4 +125,23 @@ describe('Options provide/inject', () => {
expect(vm.foo).toBe(1)
})
if (typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys)) {
it('with Symbol keys', () => {
const s = Symbol()
const vm = new Vue({
template: `<child/>`,
provide: {
[s]: 123
},
components: {
child: {
inject: { s },
template: `<div>{{ s }}</div>`
}
}
}).$mount()
expect(vm.$el.textContent).toBe('123')
})
}
})