Skip to main content

Svelte provides a variety of built-in elements. The first, <svelte:self>, allows a component to contain itself recursively.

It's useful for things like this folder tree view, where folders can contain other folders. In Folder.svelte we want to be able to do this...

Folder.svelte
{#if file.files}
	<Folder {...file}/>
{:else}
	<File {...file}/>
{/if}

...but that's impossible, because a module can't import itself. Instead, we use <svelte:self>:

Folder.svelte
{#if file.files}
	<svelte:self {...file}/>
{:else}
	<File {...file}/>
{/if}

Next: <svelte:component>

1
2
3
4
5
6
7
<script>
	import Folder from './Folder.svelte';
	import { files } from './data.js';
</script>
 
<Folder name="Home" {files} expanded />
 
initialising