Svelte 获取当前 svelte 文件的所有属性

Kok*_*zzu 3 svelte svelte-3

如何获取当前 svelte 文件的所有属性?

例如这个Component1.svelte

<script>
  let x = '';
  let y = '';
  let z = '';

  onMount(function(){
    console.log( what? ); 
    // need to print x, y, and z which set by other code
    // that using/importing this file
    // without defining one by one, was there such property?
  });
</script>
<span>{x}</span>
<div>{y}</div>
<p>{z}</p>
Run Code Online (Sandbox Code Playgroud)

使用者whatever.svelte

<script>
   import Foo from `./Component1.svelte`
</script>
<Foo x="1" y="abc">test</Foo>
Run Code Online (Sandbox Code Playgroud)

Ste*_*aes 5

您可以使用$$props它,它将返回传递给组件的所有属性。

  • 并且您将拥有没有“$$restProps”的“export”声明的所有属性:https://svelte.dev/docs#Attributes_and_props (3认同)