How to get access to +page.js data in +layout.svelte?

Viewed 30

I am fetching from an endpoint within my +page.js file in a route in SvelteKit, and have verified that the data is being returned properly. When I return the data from +page.js, I can access it using export let data in +page.svelte, however the majority of where I need this data is in +layout.svelte.

Is there a way to access the data returned by +page.js within +layout.svelte in SvelteKit?

1 Answers

You can pass data from +page.js to +layout.svelte:

+page.js

export function load() {
  return {
    customData: "testingData",
  };
}

+layout.sevlte

<script>
  import { page } from "$app/stores";
</script>

{$page.data.customData}

Docs

Related