javascript - Exporting props from SvelteKit load() function - Stack Overflow

I am trying to create a dynamically updating navbar in SvelteKit, with the currently open section forma

I am trying to create a dynamically updating navbar in SvelteKit, with the currently open section formatted accordingly. I am attempting to identify the page based on the first part of the path, as below:

__layout.svelte:

<script context="module">
    export const load = ({ page }) => {
        return {
            props: {
                currentSection: `${page.path}`.split('/')[0],
                sections: ['home', 'dashboard', 'settings']
            }
        };
    }
</script>

<div class="min-h-screen bg-gray-100">
    <Header {...props} />
    <slot />
</div>

Header.svelte

<script>
    import Menu from "$lib/nav/menu.svelte"
</script>

<Menu {...props}></Menu>

Menu.svelte

<script>
    export let sections;
    export let currentSection;
</script>

{#each sections as { section }}
    <a
        href="/{section}"
        class="{section == currentSection
            ? 'bg-gray-900 text-white'
            : 'text-gray-300 hover:bg-gray-700'} other-classes"
        >{section}</a
    >
{/each}

This is resulting in a props is not defined error, but I would have expected props to be defined since I've defined it in the return from the load() fundtion on the primary layout (based on the docs).

Do I somehow need to explicitly declare the props rather than expecting them to be available from the return of the load() function?

I am trying to create a dynamically updating navbar in SvelteKit, with the currently open section formatted accordingly. I am attempting to identify the page based on the first part of the path, as below:

__layout.svelte:

<script context="module">
    export const load = ({ page }) => {
        return {
            props: {
                currentSection: `${page.path}`.split('/')[0],
                sections: ['home', 'dashboard', 'settings']
            }
        };
    }
</script>

<div class="min-h-screen bg-gray-100">
    <Header {...props} />
    <slot />
</div>

Header.svelte

<script>
    import Menu from "$lib/nav/menu.svelte"
</script>

<Menu {...props}></Menu>

Menu.svelte

<script>
    export let sections;
    export let currentSection;
</script>

{#each sections as { section }}
    <a
        href="/{section}"
        class="{section == currentSection
            ? 'bg-gray-900 text-white'
            : 'text-gray-300 hover:bg-gray-700'} other-classes"
        >{section}</a
    >
{/each}

This is resulting in a props is not defined error, but I would have expected props to be defined since I've defined it in the return from the load() fundtion on the primary layout (based on the docs).

Do I somehow need to explicitly declare the props rather than expecting them to be available from the return of the load() function?

Share Improve this question asked Sep 5, 2021 at 15:11 HarryHarry 4,8468 gold badges42 silver badges70 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

The props are passed from the module script to the regular ponent script, this means you still need to add export let props in your layout as well.

<script context="module">
  export const load = () => {
    return {
      props: {
        test: 123
      }
    }
  }
</script>

<script>
  export let test; //
</script>

Note that this will always spread out the props, you cannot do export let props and retrieve all the props, you can however get all the props passed to a ponent using $$props

Also, the load function is only available for pages and layouts, so you will for sure have to export props in both Header and Menu as those are just regular svelte ponents.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745326154a4622666.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信