Skip to content

Exportable component functions. #15595

Answered by brunnerh
0x1618 asked this question in Q&A
Mar 23, 2025 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

You can export functions, they will be exposed on the component instance, e.g.

export const makeInvisible = () => (isVisible = false)
<script>
	import ComponentA from "./ComponentA.svelte"

	let instance = $state();
</script>

<ComponentA bind:this={instance}/>

<button onclick={() => instance.makeInvisible()}>
	Make component invisible!
</button>

Playground

There also is the <script module>, which you can export things from, but they will not have access to any component instance data. Exports from there are named exports of the component module.

import Component, { functionA, functionB, /* ... */ } from './component.svelte';

Playground

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@0x1618
Comment options

Answer selected by 0x1618
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants