From 68934997444c0047c49e419761dfad7fbc043a5d Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 6 Oct 2017 16:54:35 -0400 Subject: [PATCH] feat: rename inject alias from "name" to "from" --- src/core/instance/inject.js | 2 +- src/core/util/options.js | 6 +++--- test/unit/features/options/inject.spec.js | 23 ++++++++++++++++++++--- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/core/instance/inject.js b/src/core/instance/inject.js index dd56e58885..8bed0cd31b 100644 --- a/src/core/instance/inject.js +++ b/src/core/instance/inject.js @@ -49,7 +49,7 @@ export function resolveInject (inject: any, vm: Component): ?Object { for (let i = 0; i < keys.length; i++) { const key = keys[i] - const provideKey = inject[key].name + const provideKey = inject[key].from 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 11a586961c..8461a722f5 100644 --- a/src/core/util/options.js +++ b/src/core/util/options.js @@ -273,14 +273,14 @@ function normalizeInject (options: Object) { const normalized = options.inject = {} if (Array.isArray(inject)) { for (let i = 0; i < inject.length; i++) { - normalized[inject[i]] = { name: inject[i] } + normalized[inject[i]] = { from: inject[i] } } } else if (isPlainObject(inject)) { for (const key in inject) { const val = inject[key] normalized[key] = isPlainObject(val) - ? extend({ name: key }, val) - : { name: val } + ? extend({ from: key }, val) + : { from: val } } } } diff --git a/test/unit/features/options/inject.spec.js b/test/unit/features/options/inject.spec.js index 739f5daffe..b07d09b7a9 100644 --- a/test/unit/features/options/inject.spec.js +++ b/test/unit/features/options/inject.spec.js @@ -384,12 +384,29 @@ describe('Options provide/inject', () => { injected = [this.foo, this.bar, this.baz] } }) - expect(`Injection "foo" not found`).not.toHaveBeenWarned() - expect(`Injection "bar" not found`).not.toHaveBeenWarned() - expect(`Injection "baz" not found`).not.toHaveBeenWarned() expect(injected).toEqual([1, false, undefined]) }) + it('should support name alias and default together', () => { + const vm = new Vue({ + provide: { + FOO: 2 + } + }) + new Vue({ + parent: vm, + inject: { + foo: { from: 'FOO', default: 1 }, + bar: { default: false }, + baz: { default: undefined } + }, + created () { + injected = [this.foo, this.bar, this.baz] + } + }) + expect(injected).toEqual([2, false, undefined]) + }) + it('should use provided value even if inject has default', () => { const vm = new Vue({ provide: {