Skip to content

Commit 080c387

Browse files
jkzingyyx990803
authored andcommitted
Merge inject when extending a component (#5827)
* simply fix inject extends * add comments for normalizeInject * normalizeInect should return for non-array * remove isArray branch in resolveInject * add test case for extending injection * Create options.js * type of inject should be object now * Revert "type of inject should be object now" This reverts commit 8466a28.
1 parent 9831b40 commit 080c387

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

Diff for: src/core/instance/inject.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,14 @@ export function initInjections (vm: Component) {
3838
export function resolveInject (inject: any, vm: Component): ?Object {
3939
if (inject) {
4040
// inject is :any because flow is not smart enough to figure out cached
41-
// isArray here
42-
const isArray = Array.isArray(inject)
4341
const result = Object.create(null)
44-
const keys = isArray
45-
? inject
46-
: hasSymbol
42+
const keys = hasSymbol
4743
? Reflect.ownKeys(inject)
4844
: Object.keys(inject)
4945

5046
for (let i = 0; i < keys.length; i++) {
5147
const key = keys[i]
52-
const provideKey = isArray ? key : inject[key]
48+
const provideKey = inject[key]
5349
let source = vm
5450
while (source) {
5551
if (source._provided && provideKey in source._provided) {

Diff for: src/core/util/options.js

+15
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ strats.watch = function (parentVal: ?Object, childVal: ?Object): ?Object {
182182
*/
183183
strats.props =
184184
strats.methods =
185+
strats.inject =
185186
strats.computed = function (parentVal: ?Object, childVal: ?Object): ?Object {
186187
if (!childVal) return Object.create(parentVal || null)
187188
if (!parentVal) return childVal
@@ -247,6 +248,19 @@ function normalizeProps (options: Object) {
247248
options.props = res
248249
}
249250

251+
/**
252+
* Normalize all injections into Object-based format
253+
*/
254+
function normalizeInject (options: Object) {
255+
const inject = options.inject
256+
if (Array.isArray(inject)) {
257+
const normalized = options.inject = {}
258+
for (let i = 0; i < inject.length; i++) {
259+
normalized[inject[i]] = inject[i]
260+
}
261+
}
262+
}
263+
250264
/**
251265
* Normalize raw function directives into object format.
252266
*/
@@ -280,6 +294,7 @@ export function mergeOptions (
280294
}
281295

282296
normalizeProps(child)
297+
normalizeInject(child)
283298
normalizeDirectives(child)
284299
const extendsFrom = child.extends
285300
if (extendsFrom) {

Diff for: test/unit/features/options/inject.spec.js

+29
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,35 @@ describe('Options provide/inject', () => {
220220
})
221221
})
222222

223+
it('should extend properly', () => {
224+
const parent = Vue.extend({
225+
template: `<span/>`,
226+
inject: ['foo']
227+
})
228+
229+
const child = parent.extend({
230+
template: `<span/>`,
231+
inject: ['bar'],
232+
created () {
233+
injected = [this.foo, this.bar]
234+
}
235+
})
236+
237+
new Vue({
238+
template: `<div><parent/><child/></div>`,
239+
provide: {
240+
foo: 1,
241+
bar: false
242+
},
243+
components: {
244+
parent,
245+
child
246+
}
247+
}).$mount()
248+
249+
expect(injected).toEqual([1, false])
250+
})
251+
223252
it('should warn when injections has been modified', () => {
224253
const key = 'foo'
225254
const vm = new Vue({

0 commit comments

Comments
 (0)