最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sveltekit - Svelte page.form is undefined when destructured - Stack Overflow

programmeradmin0浏览0评论

I am building a simple form component following the Svelte documentation, which says that I can access the form action data from an external component using the page state.

When I destructure form from the page state, it is undefined.

However, I can access it if I do page.form directly.

Why is that? What am I missing?

<script lang="ts">
    import { enhance } from "$app/forms";
    import { page } from "$app/state";

  let pageDate = page; // pageDate.form is defined
  // let { form } = page; -> form is undefined
</script>

<h3>Login</h3>

{#if pageDate.form?.error}
  <p class="error">{pageDate.form?.error}</p>
{/if}

<form method="POST" action="?/login" use:enhance>
    <label>
        Email
        <input name="email" type="email">
    </label>
    <label>
        Password
        <input name="password" type="password">
    </label>
    <button>Log in</button>
</form>

I am building a simple form component following the Svelte documentation, which says that I can access the form action data from an external component using the page state.

When I destructure form from the page state, it is undefined.

However, I can access it if I do page.form directly.

Why is that? What am I missing?

<script lang="ts">
    import { enhance } from "$app/forms";
    import { page } from "$app/state";

  let pageDate = page; // pageDate.form is defined
  // let { form } = page; -> form is undefined
</script>

<h3>Login</h3>

{#if pageDate.form?.error}
  <p class="error">{pageDate.form?.error}</p>
{/if}

<form method="POST" action="?/login" use:enhance>
    <label>
        Email
        <input name="email" type="email">
    </label>
    <label>
        Password
        <input name="password" type="password">
    </label>
    <button>Log in</button>
</form>
Share Improve this question asked Mar 8 at 11:07 Loïc BosetLoïc Boset 5456 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

By destructuring, you get the property just once; it will be undefined on initial page load and stay that way.

You can make the destructuring reactive via $derived:

let { form } = $derived(page);
发布评论

评论列表(0)

  1. 暂无评论