Browse Source

support ES2015 Symbol in provide/inject

dev
Evan You 8 years ago
parent
commit
6c3e6dcdce
  1. 11
      src/core/instance/inject.js
  2. 2
      src/core/util/env.js
  3. 20
      test/unit/features/options/inject.spec.js

11
src/core/instance/inject.js

@ -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]

2
src/core/util/env.js

@ -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())
}

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

@ -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')
})
}
})

Loading…
Cancel
Save