Skip to content

Commit 526fa3b

Browse files
committed
feat(deprecation): unwrap injected refs in Options API by default, deprecate app.config.unwrapInjectedRefs
1 parent bbd8301 commit 526fa3b

File tree

3 files changed

+27
-64
lines changed

3 files changed

+27
-64
lines changed

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

-35
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,6 @@ describe('api: options', () => {
451451
}
452452
})
453453
const app = createApp(Parent)
454-
// TODO remove in 3.3
455-
app.config.unwrapInjectedRef = true
456454
const root = nodeOps.createElement('div')
457455
app.mount(root)
458456
expect(serializeInner(root)).toBe(`1`)
@@ -462,39 +460,6 @@ describe('api: options', () => {
462460
expect(serializeInner(root)).toBe(`3`)
463461
})
464462

465-
// TODO remove in 3.3
466-
test('provide/inject refs (compat)', async () => {
467-
const n = ref(0)
468-
const np = computed(() => n.value + 1)
469-
const Parent = defineComponent({
470-
provide() {
471-
return {
472-
n,
473-
np
474-
}
475-
},
476-
render: () => h(Child)
477-
})
478-
const Child = defineComponent({
479-
inject: ['n', 'np'],
480-
render(this: any) {
481-
return this.n.value + this.np.value
482-
}
483-
})
484-
const app = createApp(Parent)
485-
486-
const root = nodeOps.createElement('div')
487-
app.mount(root)
488-
expect(serializeInner(root)).toBe(`1`)
489-
490-
n.value++
491-
await nextTick()
492-
expect(serializeInner(root)).toBe(`3`)
493-
494-
expect(`injected property "n" is a ref`).toHaveBeenWarned()
495-
expect(`injected property "np" is a ref`).toHaveBeenWarned()
496-
})
497-
498463
test('provide accessing data in extends', () => {
499464
const Base = defineComponent({
500465
data() {

packages/runtime-core/src/apiCreateApp.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ export interface AppConfig {
110110
*/
111111
isCustomElement?: (tag: string) => boolean
112112

113+
// TODO remove in 3.4
113114
/**
114115
* Temporary config for opt-in to unwrap injected refs.
115-
* TODO deprecate in 3.3
116+
* @deprecated this no longer has effect. 3.3 always unwraps injected refs.
116117
*/
117118
unwrapInjectedRef?: boolean
118119
}
@@ -210,6 +211,22 @@ export function createAppAPI<HostElement>(
210211
}
211212

212213
const context = createAppContext()
214+
215+
// TODO remove in 3.4
216+
if (__DEV__) {
217+
Object.defineProperty(context.config, 'unwrapInjectedRef', {
218+
get() {
219+
return true
220+
},
221+
set() {
222+
warn(
223+
`app.config.unwrapInjectedRef has been deprecated. ` +
224+
`3.3 now alawys unwraps injected refs in Options API.`
225+
)
226+
}
227+
})
228+
}
229+
213230
const installedPlugins = new Set()
214231

215232
let isMounted = false

packages/runtime-core/src/componentOptions.ts

+9-28
Original file line numberDiff line numberDiff line change
@@ -675,12 +675,7 @@ export function applyOptions(instance: ComponentInternalInstance) {
675675
// - watch (deferred since it relies on `this` access)
676676

677677
if (injectOptions) {
678-
resolveInjections(
679-
injectOptions,
680-
ctx,
681-
checkDuplicateProperties,
682-
instance.appContext.config.unwrapInjectedRef
683-
)
678+
resolveInjections(injectOptions, ctx, checkDuplicateProperties)
684679
}
685680

686681
if (methods) {
@@ -884,8 +879,7 @@ export function applyOptions(instance: ComponentInternalInstance) {
884879
export function resolveInjections(
885880
injectOptions: ComponentInjectOptions,
886881
ctx: any,
887-
checkDuplicateProperties = NOOP as any,
888-
unwrapRef = false
882+
checkDuplicateProperties = NOOP as any
889883
) {
890884
if (isArray(injectOptions)) {
891885
injectOptions = normalizeInject(injectOptions)!
@@ -907,26 +901,13 @@ export function resolveInjections(
907901
injected = inject(opt)
908902
}
909903
if (isRef(injected)) {
910-
// TODO remove the check in 3.3
911-
if (unwrapRef) {
912-
Object.defineProperty(ctx, key, {
913-
enumerable: true,
914-
configurable: true,
915-
get: () => (injected as Ref).value,
916-
set: v => ((injected as Ref).value = v)
917-
})
918-
} else {
919-
if (__DEV__) {
920-
warn(
921-
`injected property "${key}" is a ref and will be auto-unwrapped ` +
922-
`and no longer needs \`.value\` in the next minor release. ` +
923-
`To opt-in to the new behavior now, ` +
924-
`set \`app.config.unwrapInjectedRef = true\` (this config is ` +
925-
`temporary and will not be needed in the future.)`
926-
)
927-
}
928-
ctx[key] = injected
929-
}
904+
// unwrap injected refs (ref #4196)
905+
Object.defineProperty(ctx, key, {
906+
enumerable: true,
907+
configurable: true,
908+
get: () => (injected as Ref).value,
909+
set: v => ((injected as Ref).value = v)
910+
})
930911
} else {
931912
ctx[key] = injected
932913
}

0 commit comments

Comments
 (0)