Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/core/instance/inject.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* @flow */

import { warn } from '../util/index'
import { hasOwn } from 'shared/util'
import { hasSymbol } from 'core/util/env'
import { defineReactive, observerState } from '../observer/index'

Expand Down Expand Up @@ -56,7 +55,7 @@ export function resolveInject (inject: any, vm: Component): ?Object {
}
source = source.$parent
}
if (process.env.NODE_ENV !== 'production' && !hasOwn(result, key)) {
if (process.env.NODE_ENV !== 'production' && !source) {
warn(`Injection "${key}" not found`, vm)
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/core/util/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,10 @@ 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
const ret = Object.create(null)
extend(ret, parentVal)
extend(ret, childVal)
if (childVal) extend(ret, childVal)
return ret
}
strats.provide = mergeDataOrFn
Expand Down
62 changes: 62 additions & 0 deletions test/unit/features/options/inject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,68 @@ describe('Options provide/inject', () => {
expect(injected).toEqual([1, false])
})

it('should merge from mixins properly (objects)', () => {
const mixinA = { inject: { foo: 'foo' }}
const mixinB = { inject: { bar: 'bar' }}
const child = {
mixins: [mixinA, mixinB],
template: `<span/>`,
created () {
injected = [this.foo, this.bar]
}
}
new Vue({
provide: { foo: 'foo', bar: 'bar', baz: 'baz' },
render (h) {
return h(child)
}
}).$mount()

expect(injected).toEqual(['foo', 'bar'])
})

it('should merge from mixins properly (arrays)', () => {
const mixinA = { inject: ['foo'] }
const mixinB = { inject: ['bar'] }
const child = {
mixins: [mixinA, mixinB],
inject: ['baz'],
template: `<span/>`,
created () {
injected = [this.foo, this.bar, this.baz]
}
}
new Vue({
provide: { foo: 'foo', bar: 'bar', baz: 'baz' },
render (h) {
return h(child)
}
}).$mount()

expect(injected).toEqual(['foo', 'bar', 'baz'])
})

it('should merge from mixins properly (mix of objects and arrays)', () => {
const mixinA = { inject: { foo: 'foo' }}
const mixinB = { inject: ['bar'] }
const child = {
mixins: [mixinA, mixinB],
inject: { qux: 'baz' },
template: `<span/>`,
created () {
injected = [this.foo, this.bar, this.qux]
}
}
new Vue({
provide: { foo: 'foo', bar: 'bar', baz: 'baz' },
render (h) {
return h(child)
}
}).$mount()

expect(injected).toEqual(['foo', 'bar', 'baz'])
})

it('should warn when injections has been modified', () => {
const key = 'foo'
const vm = new Vue({
Expand Down