如何在Svelte中将非字符串的动态属性分配给嵌套组件?

huangapple go评论101阅读模式
英文:

How to assign non-string dynamic attributes to a nested component in Svelte?

问题

在另一个组件中正确嵌套具有动态非字符串属性(例如数字和数组)的Svelte组件的语法如下所示:

  1. <script lang="ts">
  2. import MySimpleComponent from './MySimpleComponent.svelte';
  3. let index: number = 1;
  4. let paragraphs: string[] = [
  5. 'Nisi ab nesciunt sapiente. Et nostrum quo qui quia non.',
  6. 'Aut vel quia vel ducimus eius perferendis.'
  7. ];
  8. </script>
  9. <MySimpleComponent {index} {paragraphs} />

以上是正确的语法,将动态属性直接传递给子组件,而无需使用{ }语法。

英文:

For a Svelte component with dynamic attributes which are not strings (i.e. a number and array), for example MySimpleComponent.svelte:

  1. &lt;script lang=&quot;ts&quot;&gt;
  2. export let index : number;
  3. export let paragraphs: string[];
  4. &lt;/script&gt;
  5. &lt;h3&gt; { index } &lt;/h3&gt;
  6. {#each paragraphs as paragraph, i}
  7. &lt;p&gt; { paragraph } &lt;/p&gt;
  8. {/each}

What is the correct syntax to place that component in a different component, and specify those attributes inline? I am trying this, but it doesn't work:

  1. &lt;script lang=&quot;ts&quot;&gt;
  2. import MySimpleComponent from &#39;./MySimpleComponent.svelte&#39;;
  3. &lt;/script&gt;
  4. &lt;MySimpleComponent
  5. index = { 1 },
  6. paragraphs = {[
  7. &#39;Nisi ab nesciunt sapiente. Et nostrum quo qui quia non.&#39;,
  8. &#39;Aut vel quia vel ducimus eius perferendis.&#39;
  9. ]},
  10. /&gt;

The error is TS 2322: Type &#39;string&#39; is not assignable to type &#39;number&#39; (or to type string[]). I assume because the { } syntax is implicitly expecting a string inside, meaning { 1 } is really &#39;1&#39;, not 1.

What is the correct way to do this? Thanks.

答案1

得分: 2

The problem should be commas, props are separated only by whitespace:

  1. <MySimpleComponent
  2. index={1}
  3. paragraphs={[
  4. 'Nisi ab nesciunt sapiente. Et nostrum quo qui quia non.',
  5. 'Aut vel quia vel ducimus eius perferendis.'
  6. ]} />

(Whitespace in/around the braces and next to the = should not matter.)

Curly braces pass values verbatim without any conversion. To pass strings one would use quotes around the values with other text or invoke .toString() manually (e.g. index="1").

Note that if a property only contains a {...} expression it will not be converted to a string but again passed verbatim (see this issue for examples).

英文:

The problem should be commas, props are separated only by whitespace:

  1. &lt;MySimpleComponent
  2. index={1}
  3. paragraphs={[
  4. &#39;Nisi ab nesciunt sapiente. Et nostrum quo qui quia non.&#39;,
  5. &#39;Aut vel quia vel ducimus eius perferendis.&#39;
  6. ]} /&gt;

(Whitespace in/around the braces and next to the = should not matter.)

Curly braces pass values verbatim without any conversion. To pass strings one would use quotes around the values with other text or invoke .toString() manually (e.g. index=&quot;1&quot;).

Note that if a property only contains a {...} expression it will not be converted to a string but again passed verbatim (see this issue for examples).

huangapple
  • 本文由 发表于 2023年5月10日 22:52:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定