Skip to content

Commit

Permalink
fix(provide/inject): resolve inject properly from mixins (vuejs#6107)
Browse files Browse the repository at this point in the history
  • Loading branch information
javoski authored and yyx990803 committed Jul 19, 2017
1 parent c70addf commit b0f00e3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
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

0 comments on commit b0f00e3

Please sign in to comment.