Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close #6097: Allow defining optional inject dependency with default values #6322

Merged
merged 3 commits into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't if (!result[key]) easier to understand here?

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({})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add also an injection with a value and check that it takes precedence?

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