diff --git a/src/core/instance/inject.js b/src/core/instance/inject.js index 38fa6b108aa..ac36564d966 100644 --- a/src/core/instance/inject.js +++ b/src/core/instance/inject.js @@ -38,18 +38,14 @@ export function initInjections (vm: Component) { export function resolveInject (inject: any, vm: Component): ?Object { if (inject) { // inject is :any because flow is not smart enough to figure out cached - // isArray here - const isArray = Array.isArray(inject) const result = Object.create(null) - const keys = isArray - ? inject - : hasSymbol + const keys = hasSymbol ? Reflect.ownKeys(inject) : Object.keys(inject) for (let i = 0; i < keys.length; i++) { const key = keys[i] - const provideKey = isArray ? key : inject[key] + const provideKey = inject[key] let source = vm while (source) { if (source._provided && provideKey in source._provided) { diff --git a/src/core/util/options.js b/src/core/util/options.js index 4335b355996..99580f0443a 100644 --- a/src/core/util/options.js +++ b/src/core/util/options.js @@ -182,6 +182,7 @@ strats.watch = function (parentVal: ?Object, childVal: ?Object): ?Object { */ strats.props = strats.methods = +strats.inject = strats.computed = function (parentVal: ?Object, childVal: ?Object): ?Object { if (!childVal) return Object.create(parentVal || null) if (!parentVal) return childVal @@ -247,6 +248,19 @@ function normalizeProps (options: Object) { options.props = res } +/** + * Normalize all injections into Object-based format + */ +function normalizeInject (options: Object) { + const inject = options.inject + if (Array.isArray(inject)) { + const normalized = options.inject = {} + for (let i = 0; i < inject.length; i++) { + normalized[inject[i]] = inject[i] + } + } +} + /** * Normalize raw function directives into object format. */ @@ -280,6 +294,7 @@ export function mergeOptions ( } normalizeProps(child) + normalizeInject(child) normalizeDirectives(child) const extendsFrom = child.extends if (extendsFrom) { diff --git a/test/unit/features/options/inject.spec.js b/test/unit/features/options/inject.spec.js index 3181a19fcdc..c366918bd94 100644 --- a/test/unit/features/options/inject.spec.js +++ b/test/unit/features/options/inject.spec.js @@ -220,6 +220,35 @@ describe('Options provide/inject', () => { }) }) + it('should extend properly', () => { + const parent = Vue.extend({ + template: ``, + inject: ['foo'] + }) + + const child = parent.extend({ + template: ``, + inject: ['bar'], + created () { + injected = [this.foo, this.bar] + } + }) + + new Vue({ + template: `