Skip to content

Commit f41773f

Browse files
javoskiyyx990803
authored andcommitted
fix(provide/inject): resolve inject properly from mixins (#6107)
fix #6093
1 parent c90b140 commit f41773f

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

src/core/instance/inject.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* @flow */
22

33
import { warn } from '../util/index'
4-
import { hasOwn } from 'shared/util'
54
import { hasSymbol } from 'core/util/env'
65
import { defineReactive, observerState } from '../observer/index'
76

@@ -56,7 +55,7 @@ export function resolveInject (inject: any, vm: Component): ?Object {
5655
}
5756
source = source.$parent
5857
}
59-
if (process.env.NODE_ENV !== 'production' && !hasOwn(result, key)) {
58+
if (process.env.NODE_ENV !== 'production' && !source) {
6059
warn(`Injection "${key}" not found`, vm)
6160
}
6261
}

src/core/util/options.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,10 @@ strats.props =
201201
strats.methods =
202202
strats.inject =
203203
strats.computed = function (parentVal: ?Object, childVal: ?Object): ?Object {
204-
if (!childVal) return Object.create(parentVal || null)
205204
if (!parentVal) return childVal
206205
const ret = Object.create(null)
207206
extend(ret, parentVal)
208-
extend(ret, childVal)
207+
if (childVal) extend(ret, childVal)
209208
return ret
210209
}
211210
strats.provide = mergeDataOrFn

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

+62
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,68 @@ describe('Options provide/inject', () => {
250250
expect(injected).toEqual([1, false])
251251
})
252252

253+
it('should merge from mixins properly (objects)', () => {
254+
const mixinA = { inject: { foo: 'foo' }}
255+
const mixinB = { inject: { bar: 'bar' }}
256+
const child = {
257+
mixins: [mixinA, mixinB],
258+
template: `<span/>`,
259+
created () {
260+
injected = [this.foo, this.bar]
261+
}
262+
}
263+
new Vue({
264+
provide: { foo: 'foo', bar: 'bar', baz: 'baz' },
265+
render (h) {
266+
return h(child)
267+
}
268+
}).$mount()
269+
270+
expect(injected).toEqual(['foo', 'bar'])
271+
})
272+
273+
it('should merge from mixins properly (arrays)', () => {
274+
const mixinA = { inject: ['foo'] }
275+
const mixinB = { inject: ['bar'] }
276+
const child = {
277+
mixins: [mixinA, mixinB],
278+
inject: ['baz'],
279+
template: `<span/>`,
280+
created () {
281+
injected = [this.foo, this.bar, this.baz]
282+
}
283+
}
284+
new Vue({
285+
provide: { foo: 'foo', bar: 'bar', baz: 'baz' },
286+
render (h) {
287+
return h(child)
288+
}
289+
}).$mount()
290+
291+
expect(injected).toEqual(['foo', 'bar', 'baz'])
292+
})
293+
294+
it('should merge from mixins properly (mix of objects and arrays)', () => {
295+
const mixinA = { inject: { foo: 'foo' }}
296+
const mixinB = { inject: ['bar'] }
297+
const child = {
298+
mixins: [mixinA, mixinB],
299+
inject: { qux: 'baz' },
300+
template: `<span/>`,
301+
created () {
302+
injected = [this.foo, this.bar, this.qux]
303+
}
304+
}
305+
new Vue({
306+
provide: { foo: 'foo', bar: 'bar', baz: 'baz' },
307+
render (h) {
308+
return h(child)
309+
}
310+
}).$mount()
311+
312+
expect(injected).toEqual(['foo', 'bar', 'baz'])
313+
})
314+
253315
it('should warn when injections has been modified', () => {
254316
const key = 'foo'
255317
const vm = new Vue({

0 commit comments

Comments
 (0)