]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(deprecation): unwrap injected refs in Options API by default, deprecate app...
authorEvan You <yyx990803@gmail.com>
Fri, 21 Apr 2023 07:54:03 +0000 (15:54 +0800)
committerEvan You <yyx990803@gmail.com>
Fri, 21 Apr 2023 07:54:03 +0000 (15:54 +0800)
packages/runtime-core/__tests__/apiOptions.spec.ts
packages/runtime-core/src/apiCreateApp.ts
packages/runtime-core/src/componentOptions.ts

index eebf0bacffb639183fdd69db20750371c5e74645..a172196d3f771af554386712caf198550197b4c8 100644 (file)
@@ -451,8 +451,6 @@ describe('api: options', () => {
       }
     })
     const app = createApp(Parent)
-    // TODO remove in 3.3
-    app.config.unwrapInjectedRef = true
     const root = nodeOps.createElement('div')
     app.mount(root)
     expect(serializeInner(root)).toBe(`1`)
@@ -462,39 +460,6 @@ describe('api: options', () => {
     expect(serializeInner(root)).toBe(`3`)
   })
 
-  // TODO remove in 3.3
-  test('provide/inject refs (compat)', async () => {
-    const n = ref(0)
-    const np = computed(() => n.value + 1)
-    const Parent = defineComponent({
-      provide() {
-        return {
-          n,
-          np
-        }
-      },
-      render: () => h(Child)
-    })
-    const Child = defineComponent({
-      inject: ['n', 'np'],
-      render(this: any) {
-        return this.n.value + this.np.value
-      }
-    })
-    const app = createApp(Parent)
-
-    const root = nodeOps.createElement('div')
-    app.mount(root)
-    expect(serializeInner(root)).toBe(`1`)
-
-    n.value++
-    await nextTick()
-    expect(serializeInner(root)).toBe(`3`)
-
-    expect(`injected property "n" is a ref`).toHaveBeenWarned()
-    expect(`injected property "np" is a ref`).toHaveBeenWarned()
-  })
-
   test('provide accessing data in extends', () => {
     const Base = defineComponent({
       data() {
index 6b698ba26c24a43da0d65abc4009d12ab1e7cce2..15f7766f32308132767256fe56a48d47b8addd86 100644 (file)
@@ -110,9 +110,10 @@ export interface AppConfig {
    */
   isCustomElement?: (tag: string) => boolean
 
+  // TODO remove in 3.4
   /**
    * Temporary config for opt-in to unwrap injected refs.
-   * TODO deprecate in 3.3
+   * @deprecated this no longer has effect. 3.3 always unwraps injected refs.
    */
   unwrapInjectedRef?: boolean
 }
@@ -210,6 +211,22 @@ export function createAppAPI<HostElement>(
     }
 
     const context = createAppContext()
+
+    // TODO remove in 3.4
+    if (__DEV__) {
+      Object.defineProperty(context.config, 'unwrapInjectedRef', {
+        get() {
+          return true
+        },
+        set() {
+          warn(
+            `app.config.unwrapInjectedRef has been deprecated. ` +
+              `3.3 now alawys unwraps injected refs in Options API.`
+          )
+        }
+      })
+    }
+
     const installedPlugins = new Set()
 
     let isMounted = false
index bba5ade9ad41eb1fc07907788a15ab4863422421..527686596359da0f47a672fd2375148968a25d5c 100644 (file)
@@ -675,12 +675,7 @@ export function applyOptions(instance: ComponentInternalInstance) {
   // - watch (deferred since it relies on `this` access)
 
   if (injectOptions) {
-    resolveInjections(
-      injectOptions,
-      ctx,
-      checkDuplicateProperties,
-      instance.appContext.config.unwrapInjectedRef
-    )
+    resolveInjections(injectOptions, ctx, checkDuplicateProperties)
   }
 
   if (methods) {
@@ -884,8 +879,7 @@ export function applyOptions(instance: ComponentInternalInstance) {
 export function resolveInjections(
   injectOptions: ComponentInjectOptions,
   ctx: any,
-  checkDuplicateProperties = NOOP as any,
-  unwrapRef = false
+  checkDuplicateProperties = NOOP as any
 ) {
   if (isArray(injectOptions)) {
     injectOptions = normalizeInject(injectOptions)!
@@ -907,26 +901,13 @@ export function resolveInjections(
       injected = inject(opt)
     }
     if (isRef(injected)) {
-      // TODO remove the check in 3.3
-      if (unwrapRef) {
-        Object.defineProperty(ctx, key, {
-          enumerable: true,
-          configurable: true,
-          get: () => (injected as Ref).value,
-          set: v => ((injected as Ref).value = v)
-        })
-      } else {
-        if (__DEV__) {
-          warn(
-            `injected property "${key}" is a ref and will be auto-unwrapped ` +
-              `and no longer needs \`.value\` in the next minor release. ` +
-              `To opt-in to the new behavior now, ` +
-              `set \`app.config.unwrapInjectedRef = true\` (this config is ` +
-              `temporary and will not be needed in the future.)`
-          )
-        }
-        ctx[key] = injected
-      }
+      // unwrap injected refs (ref #4196)
+      Object.defineProperty(ctx, key, {
+        enumerable: true,
+        configurable: true,
+        get: () => (injected as Ref).value,
+        set: v => ((injected as Ref).value = v)
+      })
     } else {
       ctx[key] = injected
     }