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

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

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

问题

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

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

<MySimpleComponent {index} {paragraphs} />

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

英文:

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

&lt;script lang=&quot;ts&quot;&gt;
	export let index : number;
	export let paragraphs: string[];
&lt;/script&gt;

&lt;h3&gt; { index } &lt;/h3&gt;
{#each paragraphs as paragraph, i}
	&lt;p&gt; { paragraph } &lt;/p&gt;	
{/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:

&lt;script lang=&quot;ts&quot;&gt;
	import MySimpleComponent from &#39;./MySimpleComponent.svelte&#39;;
&lt;/script&gt;

&lt;MySimpleComponent
	index = { 1 },
	paragraphs = {[
		&#39;Nisi ab nesciunt sapiente. Et nostrum quo qui quia non.&#39;,
		&#39;Aut vel quia vel ducimus eius perferendis.&#39; 
	]},
/&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:

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

(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:

&lt;MySimpleComponent
    index={1}
    paragraphs={[
        &#39;Nisi ab nesciunt sapiente. Et nostrum quo qui quia non.&#39;,
        &#39;Aut vel quia vel ducimus eius perferendis.&#39; 
    ]} /&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:

确定