Do dynamic props exist in Svelte 3

2020-03-30 05:32发布

问题:

When I iterate over a dynamic component like:

<svelte:component collection={collection} uid={uid} this={upload_component} 
     bind:action={restart}/>

Is it possible to use a set of dynamic props for each component. Each component having it's own set of prop names and prop values.

Solution example:

<script>
    import Info from './Info.svelte';

    const pkgs = [{
        name: 'svelte',
        version: 3,
        speed: 'blazing',
        website: 'https://svelte.dev'
    }, ];
</script>

<Info {...pkgs[0]}/>

More in this Rich Harris answer here.

回答1:

Yes. You need spread props:

<svelte:component this={upload_component} bind:action={restart} {...someprops}/>

(Note that bindings and event listeners are not included in those props — but you can always pass down a callback function among your props.)



标签: svelte