我們也可以直接將事件處理器展開到元素上。在這裡,我們在 App.svelte
中定義了一個 onclick
處理器 — 我們需要做的就是將 props 傳遞給 BigRedButton.svelte
中的 <button>
BigRedButton
<button {...props}>
Push
</button>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script>
import BigRedButton from './BigRedButton.svelte';
import horn from './horn.mp3';
const audio = new Audio();
audio.src = horn;
function honk() {
audio.load();
audio.play();
}
</script>
<BigRedButton onclick={honk} />