mirror of
https://gitee.com/vuejs/vue.git
synced 2024-12-04 21:17:55 +08:00
support ES2015 Symbol in provide/inject
This commit is contained in:
parent
135f06d25f
commit
6c3e6dcdce
@ -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]
|
||||
|
@ -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())
|
||||
}
|
||||
|
||||
|
@ -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…
Reference in New Issue
Block a user