0

Component.svelte:

<script>
export function test(){
    return "test";
}
</script>

App.svelte:

<script>
    import { test } from "./Component.svelte" // error;
     
    let testValue = $state(test())
</script>

<h1>{testValue}</h1>

Is there a way to export a function from the component? But without actually mounting the component

Or is there some way to replicate vuejs portal, but without mounting the full component?

1 Answer 1

3

You can export function from component in the script context="module". Then you can import function without mounting component.

<script context="module">
   export function test(){
     return "test";
   }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

fyi.. in latest svelte i get a deprecation warning. Looks like they replaced this with module attribute. Still works tho!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.