Skip to content

Commit

Permalink
feat(inject): support providing default values for injections (vuejs#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pdanpdan authored and hefeng committed Jan 25, 2019
1 parent 1acb528 commit de4cc3e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/core/instance/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
const provideKey = inject[key].name
let source = vm
while (source) {
if (source._provided && provideKey in source._provided) {
Expand All @@ -58,8 +58,15 @@ export function resolveInject (inject: any, vm: Component): ?Object {
}
source = source.$parent
}
if (process.env.NODE_ENV !== 'production' && !source) {
warn(`Injection "${key}" not found`, vm)
if (!source) {
if ('default' in inject[key]) {
const provideDefault = inject[key].default
result[key] = typeof provideDefault === 'function'
? provideDefault.call(vm)
: provideDefault
} else if (process.env.NODE_ENV !== 'production') {
warn(`Injection "${key}" not found`, vm)
}
}
}
return result
Expand Down
11 changes: 9 additions & 2 deletions src/core/util/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,17 @@ function normalizeProps (options: Object) {
*/
function normalizeInject (options: Object) {
const inject = options.inject
const normalized = options.inject = {}
if (Array.isArray(inject)) {
const normalized = options.inject = {}
for (let i = 0; i < inject.length; i++) {
normalized[inject[i]] = inject[i]
normalized[inject[i]] = { name: 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 }
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions test/unit/features/options/inject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,45 @@ describe('Options provide/inject', () => {
expect(`Injection "__ob__" not found`).not.toHaveBeenWarned()
})

// Github issue #6097
it('should not warn when injections cannot be found but have default value', () => {
const vm = new Vue({})
new Vue({
parent: vm,
inject: {
foo: { default: 1 },
bar: { default: false },
baz: { default: undefined }
},
created () {}
})
expect(`Injection "foo" not found`).not.toHaveBeenWarned()
expect(`Injection "bar" not found`).not.toHaveBeenWarned()
expect(`Injection "baz" not found`).not.toHaveBeenWarned()
})

it('should use provided value even if inject has default', () => {
const vm = new Vue({
provide: {
foo: 1,
bar: false,
baz: undefined
}
})
new Vue({
parent: vm,
inject: {
foo: { default: 2 },
bar: { default: 2 },
baz: { default: 2 }
},
created () {
injected = [this.foo, this.bar, this.baz]
}
})
expect(injected).toEqual([1, false, undefined])
})

// Github issue #6008
it('should merge provide from mixins (objects)', () => {
const mixinA = { provide: { foo: 'foo' }}
Expand Down

0 comments on commit de4cc3e

Please sign in to comment.