]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
chore(runtime-core): warn if use a non-ref to hold the element reference in DEV ...
authoredison <daiwei521@126.com>
Fri, 11 Oct 2024 02:53:45 +0000 (10:53 +0800)
committerGitHub <noreply@github.com>
Fri, 11 Oct 2024 02:53:45 +0000 (10:53 +0800)
close #12029

packages/runtime-core/__tests__/rendererTemplateRef.spec.ts
packages/runtime-core/src/rendererTemplateRef.ts

index 799108dca8ae3829c7b8457d9b18e02b626c415f..64cb9c63014a2295f67833f193f7caad8d55299e 100644 (file)
@@ -217,6 +217,7 @@ describe('api: template refs', () => {
     }
     render(h(Comp), root)
     expect(state.refKey).toBe(root.children[0])
+    expect('Template ref "refKey" used on a non-ref value').toHaveBeenWarned()
   })
 
   test('multiple root refs', () => {
index 1ffe3035794da8adbee967ad749c0f0eec0df33b..bffe1a25321220c6df2d483a45479041d98f9e2c 100644 (file)
@@ -69,8 +69,17 @@ export function setRef(
     setupState === EMPTY_OBJ
       ? () => false
       : (key: string) => {
-          if (__DEV__ && knownTemplateRefs.has(rawSetupState[key] as any)) {
-            return false
+          if (__DEV__) {
+            if (hasOwn(rawSetupState, key) && !isRef(rawSetupState[key])) {
+              warn(
+                `Template ref "${key}" used on a non-ref value. ` +
+                  `It will not work in the production build.`,
+              )
+            }
+
+            if (knownTemplateRefs.has(rawSetupState[key] as any)) {
+              return false
+            }
           }
           return hasOwn(rawSetupState, key)
         }