From 1c4439e8179b0f8899aca13d45720b7dd0a8b580 Mon Sep 17 00:00:00 2001 From: jkzing Date: Mon, 5 Jun 2017 23:07:12 +0800 Subject: [PATCH 1/8] simply fix inject extends --- src/core/util/options.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/core/util/options.js b/src/core/util/options.js index 53f96830165..740d1c85587 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,27 @@ function normalizeProps (options: Object) { options.props = res } +function normalizeInject (options: Object) { + const inject = options.inject + if (!inject) return + let res = {} + let i, val + if (Array.isArray(inject)) { + i = inject.length + while (i--) { + val = inject[i] + if (typeof val === 'string') { + res[val] = val + } else if (process.env.NODE_ENV !== 'production') { + warn('inject must be strings when using array syntax.') + } + } + } else if (isPlainObject(inject)) { + res = options.inject + } + options.inject = res +} + /** * Normalize raw function directives into object format. */ @@ -280,6 +302,7 @@ export function mergeOptions ( } normalizeProps(child) + normalizeInject(child) normalizeDirectives(child) const extendsFrom = child.extends if (extendsFrom) { From bfda50360d8d8f722993ee2537611c9e650f7288 Mon Sep 17 00:00:00 2001 From: jkzing Date: Mon, 5 Jun 2017 23:18:33 +0800 Subject: [PATCH 2/8] add comments for normalizeInject --- src/core/util/options.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/core/util/options.js b/src/core/util/options.js index 740d1c85587..6759d89aea3 100644 --- a/src/core/util/options.js +++ b/src/core/util/options.js @@ -248,6 +248,9 @@ function normalizeProps (options: Object) { options.props = res } +/** + * Normalize all injections into Object-based format + */ function normalizeInject (options: Object) { const inject = options.inject if (!inject) return From 574162ea85625ab52c08d11d5c21697b490b9d32 Mon Sep 17 00:00:00 2001 From: jkzing Date: Tue, 6 Jun 2017 16:53:38 +0800 Subject: [PATCH 3/8] normalizeInect should return for non-array --- src/core/util/options.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/core/util/options.js b/src/core/util/options.js index 6759d89aea3..c54b87a1f4e 100644 --- a/src/core/util/options.js +++ b/src/core/util/options.js @@ -253,21 +253,17 @@ function normalizeProps (options: Object) { */ function normalizeInject (options: Object) { const inject = options.inject - if (!inject) return - let res = {} - let i, val - if (Array.isArray(inject)) { - i = inject.length - while (i--) { - val = inject[i] - if (typeof val === 'string') { - res[val] = val - } else if (process.env.NODE_ENV !== 'production') { - warn('inject must be strings when using array syntax.') - } + if (!Array.isArray(inject)) return + const res = {} + let i = inject.length + let val + while (i--) { + val = inject[i] + if (typeof val === 'string') { + res[val] = val + } else if (process.env.NODE_ENV !== 'production') { + warn('inject must be strings when using array syntax.') } - } else if (isPlainObject(inject)) { - res = options.inject } options.inject = res } From 01db8fe53fb690952908a3dda198241fa1e7a22e Mon Sep 17 00:00:00 2001 From: jkzing Date: Tue, 6 Jun 2017 21:22:33 +0800 Subject: [PATCH 4/8] remove isArray branch in resolveInject --- src/core/instance/inject.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) 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) { From 7a13fd6b0d0a7c5d83bb303770ccb244616e8bb7 Mon Sep 17 00:00:00 2001 From: jkzing Date: Tue, 6 Jun 2017 22:03:45 +0800 Subject: [PATCH 5/8] add test case for extending injection --- test/unit/features/options/inject.spec.js | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) 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: `
`, + provide: { + foo: 1, + bar: false + }, + components: { + parent, + child + } + }).$mount() + + expect(injected).toEqual([1, false]) + }) + it('should warn when injections has been modified', () => { const key = 'foo' const vm = new Vue({ From 2e3504666b359a4b3d238cfd04a4c604f601b0ea Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 15 Jun 2017 22:01:44 +0800 Subject: [PATCH 6/8] Create options.js --- src/core/util/options.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/core/util/options.js b/src/core/util/options.js index c54b87a1f4e..83121ac584f 100644 --- a/src/core/util/options.js +++ b/src/core/util/options.js @@ -253,19 +253,12 @@ function normalizeProps (options: Object) { */ function normalizeInject (options: Object) { const inject = options.inject - if (!Array.isArray(inject)) return - const res = {} - let i = inject.length - let val - while (i--) { - val = inject[i] - if (typeof val === 'string') { - res[val] = val - } else if (process.env.NODE_ENV !== 'production') { - warn('inject must be strings when using array syntax.') + if (Array.isArray(inject)) { + const normalized = options.inject = {} + for (let i = 0; i < inject.length; i++) { + normalized[inject[i]] = inject[i] } } - options.inject = res } /** From 8466a2866b868de00f755b80e5b3a3dc8bdc2d86 Mon Sep 17 00:00:00 2001 From: jkzing Date: Thu, 15 Jun 2017 22:02:30 +0800 Subject: [PATCH 7/8] type of inject should be object now --- src/core/instance/inject.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/instance/inject.js b/src/core/instance/inject.js index ac36564d966..84b2a65675e 100644 --- a/src/core/instance/inject.js +++ b/src/core/instance/inject.js @@ -35,9 +35,8 @@ export function initInjections (vm: Component) { } } -export function resolveInject (inject: any, vm: Component): ?Object { +export function resolveInject (inject: ?Object, vm: Component): ?Object { if (inject) { - // inject is :any because flow is not smart enough to figure out cached const result = Object.create(null) const keys = hasSymbol ? Reflect.ownKeys(inject) From a4fade54114e3c80018f506788fb677bfe26eb1a Mon Sep 17 00:00:00 2001 From: jkzing Date: Thu, 15 Jun 2017 22:08:17 +0800 Subject: [PATCH 8/8] Revert "type of inject should be object now" This reverts commit 8466a2866b868de00f755b80e5b3a3dc8bdc2d86. --- src/core/instance/inject.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/instance/inject.js b/src/core/instance/inject.js index 84b2a65675e..ac36564d966 100644 --- a/src/core/instance/inject.js +++ b/src/core/instance/inject.js @@ -35,8 +35,9 @@ export function initInjections (vm: Component) { } } -export function resolveInject (inject: ?Object, vm: Component): ?Object { +export function resolveInject (inject: any, vm: Component): ?Object { if (inject) { + // inject is :any because flow is not smart enough to figure out cached const result = Object.create(null) const keys = hasSymbol ? Reflect.ownKeys(inject)