Browse Source

fix(provide/inject): resolve inject properly from mixins (#6107)

fix #6093
dev
AchillesJ 7 years ago
committed by Evan You
parent
commit
b0f00e31e7
  1. 3
      src/core/instance/inject.js
  2. 3
      src/core/util/options.js
  3. 62
      test/unit/features/options/inject.spec.js

3
src/core/instance/inject.js

@ -1,7 +1,6 @@
/* @flow */ /* @flow */
import { warn } from '../util/index' import { warn } from '../util/index'
import { hasOwn } from 'shared/util'
import { hasSymbol } from 'core/util/env' import { hasSymbol } from 'core/util/env'
import { defineReactive, observerState } from '../observer/index' import { defineReactive, observerState } from '../observer/index'
@ -56,7 +55,7 @@ export function resolveInject (inject: any, vm: Component): ?Object {
} }
source = source.$parent 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) warn(`Injection "${key}" not found`, vm)
} }
} }

3
src/core/util/options.js

@ -201,11 +201,10 @@ strats.props =
strats.methods = strats.methods =
strats.inject = strats.inject =
strats.computed = function (parentVal: ?Object, childVal: ?Object): ?Object { strats.computed = function (parentVal: ?Object, childVal: ?Object): ?Object {
if (!childVal) return Object.create(parentVal || null)
if (!parentVal) return childVal if (!parentVal) return childVal
const ret = Object.create(null) const ret = Object.create(null)
extend(ret, parentVal) extend(ret, parentVal)
extend(ret, childVal) if (childVal) extend(ret, childVal)
return ret return ret
} }
strats.provide = mergeDataOrFn strats.provide = mergeDataOrFn

62
test/unit/features/options/inject.spec.js

@ -250,6 +250,68 @@ describe('Options provide/inject', () => {
expect(injected).toEqual([1, false]) 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', () => { it('should warn when injections has been modified', () => {
const key = 'foo' const key = 'foo'
const vm = new Vue({ const vm = new Vue({

Loading…
Cancel
Save