Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SvelteKit: Pass Data From +Layout.svelte To +page.svelte SPA (static) application

I’m struggling with the latest version of SvelteKit, the docs available only works with SSR, and I’m developing SPA app (static page), so, what is the way to pass data from my +layout.svelte to +page.svelte?.

The documentation says that with load function from page.js (I’ve already set the SSR=false, and I understood that page.js is for SSR), but that doesn’t work with SPA, and if I have the load function from the layout it’s seems not work.

Aditionaly I want to trigger a function from my +page.svelte that is in the layout page.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any ideas?

here my try

//+layout.svelte
<script>
    export function load() {
        return {
            data: { title: 'default title' }
        };
    }
    export let data;
</script>

//+page.svelte
<script>
    export let data;
    console.log(data.title); //undefined
</script>

the docs says that don’t use: <script context="module">, and I don’t want to use the store becouse I think that sholud be a better way.

>Solution :

For static sites I would create a store and set it as a context in the layout. Pages then can get said context and interact with it.

Using a store is necessary if the contents require reactivity (i.e. are changed) and the page or layout needs to update.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading