Skip to content

Commit 16765db

Browse files
committed
separate provide/inject resolve timing to allow data/props to rely on injections
1 parent 3294eba commit 16765db

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/core/instance/init.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { initProxy } from './proxy'
66
import { initState } from './state'
77
import { initRender } from './render'
88
import { initEvents } from './events'
9-
import { initInjections } from './inject'
109
import { initLifecycle, callHook } from './lifecycle'
10+
import { initProvide, initInjections } from './inject'
1111
import { extend, mergeOptions, formatComponentName } from '../util/index'
1212

1313
let uid = 0
@@ -49,8 +49,9 @@ export function initMixin (Vue: Class<Component>) {
4949
initEvents(vm)
5050
initRender(vm)
5151
callHook(vm, 'beforeCreate')
52+
initInjections(vm) // resolve injections before data/props
5253
initState(vm)
53-
initInjections(vm)
54+
initProvide(vm) // resolve provide after data/props
5455
callHook(vm, 'created')
5556

5657
/* istanbul ignore if */

src/core/instance/inject.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
import { hasSymbol } from 'core/util/env'
44

5-
export function initInjections (vm: Component) {
5+
export function initProvide (vm: Component) {
66
const provide = vm.$options.provide
7-
const inject: any = vm.$options.inject
87
if (provide) {
98
vm._provided = typeof provide === 'function'
109
? provide.call(vm)
1110
: provide
1211
}
12+
}
13+
14+
export function initInjections (vm: Component) {
15+
const inject: any = vm.$options.inject
1316
if (inject) {
1417
// inject is :any because flow is not smart enough to figure out cached
1518
// isArray here

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,33 @@ describe('Options provide/inject', () => {
115115
expect(injected).toEqual([false, 2])
116116
})
117117

118-
it('self-inject', () => {
118+
it('inject before resolving data/props', () => {
119119
const vm = new Vue({
120120
provide: {
121121
foo: 1
122+
}
123+
})
124+
125+
const child = new Vue({
126+
parent: vm,
127+
inject: ['foo'],
128+
data () {
129+
return {
130+
bar: this.foo + 1
131+
}
122132
},
123-
inject: ['foo']
133+
props: {
134+
baz: {
135+
default () {
136+
return this.foo + 2
137+
}
138+
}
139+
}
124140
})
125141

126-
expect(vm.foo).toBe(1)
142+
expect(child.foo).toBe(1)
143+
expect(child.bar).toBe(2)
144+
expect(child.baz).toBe(3)
127145
})
128146

129147
if (typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys)) {

0 commit comments

Comments
 (0)