Skip to content

Commit 58c31e3

Browse files
committed
feat(runtime-core): support using inject() inside props default functions
1 parent 985bd2b commit 58c31e3

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

packages/runtime-core/__tests__/componentProps.spec.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {
88
defineComponent,
99
ref,
1010
serializeInner,
11-
createApp
11+
createApp,
12+
provide,
13+
inject
1214
} from '@vue/runtime-test'
1315
import { render as domRender, nextTick } from 'vue'
1416

@@ -212,6 +214,32 @@ describe('component props', () => {
212214
expect(defaultFn).toHaveBeenCalledTimes(1)
213215
})
214216

217+
test('using inject in default value factory', () => {
218+
const Child = defineComponent({
219+
props: {
220+
test: {
221+
default: () => inject('test', 'default')
222+
}
223+
},
224+
setup(props) {
225+
return () => {
226+
return h('div', props.test)
227+
}
228+
}
229+
})
230+
231+
const Comp = {
232+
setup() {
233+
provide('test', 'injected')
234+
return () => h(Child)
235+
}
236+
}
237+
238+
const root = nodeOps.createElement('div')
239+
render(h(Comp), root)
240+
expect(serializeInner(root)).toBe(`<div>injected</div>`)
241+
})
242+
215243
test('optimized props updates', async () => {
216244
const Child = defineComponent({
217245
props: ['foo'],

packages/runtime-core/src/componentProps.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import {
2727
Data,
2828
ComponentInternalInstance,
2929
ComponentOptions,
30-
ConcreteComponent
30+
ConcreteComponent,
31+
setCurrentInstance
3132
} from './component'
3233
import { isEmitListener } from './componentEmits'
3334
import { InternalObjectKey } from './vnode'
@@ -179,7 +180,8 @@ export function updateProps(
179180
options,
180181
rawCurrentProps,
181182
camelizedKey,
182-
value
183+
value,
184+
instance
183185
)
184186
}
185187
} else {
@@ -214,7 +216,8 @@ export function updateProps(
214216
options,
215217
rawProps || EMPTY_OBJ,
216218
key,
217-
undefined
219+
undefined,
220+
instance
218221
)
219222
}
220223
} else {
@@ -277,7 +280,8 @@ function setFullProps(
277280
options!,
278281
rawCurrentProps,
279282
key,
280-
rawCurrentProps[key]
283+
rawCurrentProps[key],
284+
instance
281285
)
282286
}
283287
}
@@ -287,18 +291,22 @@ function resolvePropValue(
287291
options: NormalizedProps,
288292
props: Data,
289293
key: string,
290-
value: unknown
294+
value: unknown,
295+
instance: ComponentInternalInstance
291296
) {
292297
const opt = options[key]
293298
if (opt != null) {
294299
const hasDefault = hasOwn(opt, 'default')
295300
// default values
296301
if (hasDefault && value === undefined) {
297302
const defaultValue = opt.default
298-
value =
299-
opt.type !== Function && isFunction(defaultValue)
300-
? defaultValue(props)
301-
: defaultValue
303+
if (opt.type !== Function && isFunction(defaultValue)) {
304+
setCurrentInstance(instance)
305+
value = defaultValue(props)
306+
setCurrentInstance(null)
307+
} else {
308+
value = defaultValue
309+
}
302310
}
303311
// boolean casting
304312
if (opt[BooleanFlags.shouldCast]) {

0 commit comments

Comments
 (0)