跳至主要內容

$host

當將元件編譯為自訂元素時,$host 符文可讓您存取主機元素,以便您(例如)發送自訂事件 (範例)

步進器
<svelte:options customElement="my-stepper" />

<script>
	function dispatch(type) {
		$host().dispatchEvent(new CustomEvent(type));
	}
</script>

<button onclick={() => dispatch('decrement')}>decrement</button>
<button onclick={() => dispatch('increment')}>increment</button>
應用程式
<script>
	import './Stepper.svelte';

	let count = $state(0);
</script>

<my-stepper
	ondecrement={() => count -= 1}
	onincrement={() => count += 1}
></my-stepper>

<p>count: {count}</p>

在 GitHub 上編輯此頁面

上一個 下一個