]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(v-model): fix incorrect codegen for non-ref bindings
authorEvan You <yyx990803@gmail.com>
Thu, 10 Nov 2022 07:29:17 +0000 (15:29 +0800)
committerEvan You <yyx990803@gmail.com>
Thu, 10 Nov 2022 07:32:58 +0000 (15:32 +0800)
fix #6241

packages/compiler-core/src/transforms/vModel.ts
packages/compiler-sfc/__tests__/compileScript.spec.ts

index 16995994699d3f68513c57c1c71c6a695bbea61f..bf5e69beb2c677e9b6ad250795c9121954647eb6 100644 (file)
@@ -48,8 +48,9 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
   const maybeRef =
     !__BROWSER__ &&
     context.inline &&
-    bindingType &&
-    bindingType !== BindingTypes.SETUP_CONST
+    (bindingType === BindingTypes.SETUP_LET ||
+      bindingType === BindingTypes.SETUP_REF ||
+      bindingType === BindingTypes.SETUP_MAYBE_REF)
 
   if (
     !expString.trim() ||
index bf562defd241c536d53704073fef6c054852df3d..b0a6c115e52265f037e799c50caaa618ab691ab0 100644 (file)
@@ -673,6 +673,26 @@ defineExpose({ foo: 123 })
       assertCode(content)
     })
 
+    test('v-model should not generate ref assignment code for non-setup bindings', () => {
+      const { content } = compile(
+        `<script setup>
+        import { ref } from 'vue'
+        const count = ref(0)
+        </script>
+        <script>
+        export default {
+          data() { return { foo: 123 } }
+        }
+        </script>
+        <template>
+          <input v-model="foo">
+        </template>
+        `,
+        { inlineTemplate: true }
+      )
+      expect(content).not.toMatch(`_isRef(foo)`)
+    })
+
     test('template assignment expression codegen', () => {
       const { content } = compile(
         `<script setup>