Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

Svelte Conditional Component rendering

I have a basic if/else in the app to show either a preloader or a component.

{#if preloading}
    <video />
{:else}
    <svelte:component this={component} on:preload={preload}/>
{/if}

It starts with displaying the component correctly, when the preload flag is set, it shows the video, but it shows it over the svelte:component rather than replacing it.

I tried using {#key} to force a rerender by using a number as an id and incrementing it when preload is set, but the component is still there.

Any ideas?

question from:https://stackoverflow.com/questions/65905277/svelte-conditional-component-rendering

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try doing it like in the example page:

<script>
  import video from "./somewhere.svelte"
  import component from "./somewhereelse.svelte" 
</script>

<svelte:component this={preloading ? video : component} on:preload={preload}/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...