Kaynağa Gözat

fix(compile-sfc): register props destructure rest id as setup bindings (#10888)

close #10885
edison 1 yıl önce
ebeveyn
işleme
b2b5f57c2c

+ 16 - 0
packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap

@@ -304,3 +304,19 @@ return () => {}
 
 
 }"
 }"
 `;
 `;
+
+exports[`sfc reactive props destructure > rest spread non-inline 1`] = `
+"import { createPropsRestProxy as _createPropsRestProxy } from 'vue'
+
+export default {
+  props: ['foo', 'bar'],
+  setup(__props, { expose: __expose }) {
+  __expose();
+
+      const rest = _createPropsRestProxy(__props, ["foo"])
+      
+return { rest }
+}
+
+}"
+`;

+ 21 - 0
packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts

@@ -265,6 +265,27 @@ describe('sfc reactive props destructure', () => {
     })
     })
   })
   })
 
 
+  test('rest spread non-inline', () => {
+    const { content, bindings } = compile(
+      `
+      <script setup>
+      const { foo, ...rest } = defineProps(['foo', 'bar'])
+      </script>
+      <template>{{ rest.bar }}</template>
+    `,
+      { inlineTemplate: false },
+    )
+    expect(content).toMatch(
+      `const rest = _createPropsRestProxy(__props, ["foo"])`,
+    )
+    assertCode(content)
+    expect(bindings).toStrictEqual({
+      foo: BindingTypes.PROPS,
+      bar: BindingTypes.PROPS,
+      rest: BindingTypes.SETUP_REACTIVE_CONST,
+    })
+  })
+
   // #6960
   // #6960
   test('computed static key', () => {
   test('computed static key', () => {
     const { content, bindings } = compile(`
     const { content, bindings } = compile(`

+ 7 - 1
packages/compiler-sfc/src/compileScript.ts

@@ -523,8 +523,14 @@ export function compileScript(
             )
             )
           }
           }
 
 
-          // defineProps / defineEmits
+          // defineProps
           const isDefineProps = processDefineProps(ctx, init, decl.id)
           const isDefineProps = processDefineProps(ctx, init, decl.id)
+          if (ctx.propsDestructureRestId) {
+            setupBindings[ctx.propsDestructureRestId] =
+              BindingTypes.SETUP_REACTIVE_CONST
+          }
+
+          // defineEmits
           const isDefineEmits =
           const isDefineEmits =
             !isDefineProps && processDefineEmits(ctx, init, decl.id)
             !isDefineProps && processDefineEmits(ctx, init, decl.id)
           !isDefineEmits &&
           !isDefineEmits &&