メインコンテンツへスキップ

イベントハンドラを要素に直接スプレッドすることもできます。ここでは、App.svelteonclickハンドラを定義しました。必要なのは、BigRedButton.svelte<button>にpropsを渡すことだけです。

BigRedButton
<button {...props}>
	Push
</button>

GitHubでこのページを編集する

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} />