使用 use:enhance
,我們可以做得比僅僅模擬瀏覽器的原生行為還要多。透過提供回呼函式,我們可以加入像是待處理狀態和樂觀 UI 的功能。讓我們透過在兩個動作中新增人為延遲來模擬緩慢的網路
src/routes/+page.server
export const actions = {
create: async ({ cookies, request }) => {
await new Promise((fulfil) => setTimeout(fulfil, 1000));
...
},
delete: async ({ cookies, request }) => {
await new Promise((fulfil) => setTimeout(fulfil, 1000));
...
}
};
當我們建立或刪除項目時,現在需要整整一秒鐘 UI 才會更新,讓使用者懷疑他們是否搞砸了。為了解決這個問題,加入一些本地狀態...
src/routes/+page
<script>
import { fly, slide } from 'svelte/transition';
import { enhance } from '$app/forms';
let { data, form } = $props();
let creating = $state(false);
let deleting = $state([]);
</script>
...並在第一個 use:enhance
內切換 creating
src/routes/+page
<form
method="POST"
action="?/create"
use:enhance={() => {
creating = true;
return async ({ update }) => {
await update();
creating = false;
};
}}
>
<label>
add a todo:
<input
disabled={creating}
name="description"
value={form?.description ?? ''}
autocomplete="off"
required
/>
</label>
</form>
然後我們可以在儲存資料時顯示訊息
src/routes/+page
<ul class="todos">
<!-- ... -->
</ul>
{#if creating}
<span class="saving">saving...</span>
{/if}
在刪除的情況下,我們實際上不需要等待伺服器驗證任何內容 — 我們可以直接更新 UI
src/routes/+page
<ul class="todos">
{#each data.todos.filter((todo) => !deleting.includes(todo.id)) as todo (todo.id)}
<li in:fly={{ y: 20 }} out:slide>
<form
method="POST"
action="?/delete"
use:enhance={() => {
deleting = [...deleting, todo.id];
return async ({ update }) => {
await update();
deleting = deleting.filter((id) => id !== todo.id);
};
}}
>
<input type="hidden" name="id" value={todo.id} />
<button aria-label="Mark as complete">✔</button>
{todo.description}
</form>
</li>
{/each}
</ul>
use:enhance
非常可自訂 — 你可以cancel()
提交、處理重新導向、控制是否重設表單等等。詳細資訊請參閱文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<script>
import { fly, slide } from 'svelte/transition';
import { enhance } from '$app/forms';
let { data, form } = $props();
</script>
<div class="centered">
<h1>todos</h1>
{#if form?.error}
<p class="error">{form.error}</p>
{/if}
<form method="POST" action="?/create" use:enhance>
<label>
add a todo:
<input
name="description"
value={form?.description ?? ''}
autocomplete="off"
required
/>
</label>
</form>
<ul class="todos">
{#each data.todos as todo (todo.id)}
<li in:fly={{ y: 20 }} out:slide>
<form method="POST" action="?/delete" use:enhance>
<input type="hidden" name="id" value={todo.id} />
<span>{todo.description}</span>
<button aria-label="Mark as complete"></button>
</form>
</li>
{/each}
</ul>
</div>
<style>
.centered {
max-width: 20em;
margin: 0 auto;
}
label {
width: 100%;
}
input {
flex: 1;
}
span {
flex: 1;
}
button {
border: none;
background: url(./remove.svg) no-repeat 50% 50%;
background-size: 1rem 1rem;
cursor: pointer;
height: 100%;
aspect-ratio: 1;
opacity: 0.5;
transition: opacity 0.2s;
}
button:hover {
opacity: 1;
}
.saving {
opacity: 0.5;
}
</style>