Sveltekit: Import component dynamically
Thu Mar 07 2024 01:44:59 GMT+0000 (Coordinated Universal Time)
Saved by
@marcopinero
#javascript
#sveltekit
/*** +page.svelte **/
<script>
import { onMount } from 'svelte';
let Thing;
const sleep = ms => new Promise(f => setTimeout(f, ms));
onMount(async () => {
await sleep(1000); // simulate network delay
Thing = (await import('./Thing.svelte')).default;
});
</script>
<svelte:component this={Thing} answer={42}>
<p>some slotted content</p>
</svelte:component>
/*** Thing.svelte ***/
<script>
export let answer;
</script>
<p>the answer is {answer}</p>
<slot></slot>
content_copyCOPY
Comments