0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Svelte 子→親 ボタンで値渡し

Last updated at Posted at 2023-01-15

  1. 子コンポーネントに on:xxxx でイベント定義
oya
<script>
	import NumberButton from './NumberButton.svelte'

    function show(e) {
		alert(e.detail.num)
    }

</script>
<main>
	<NumberButton number="1" on:show={ show }></NumberButton>
</main>

  1. createEventDispatcher をインポートする
  2. _dispatch()で親側で定義したon:xxxのイベント名とオブジェクトを渡す
ko
<script>
	import { createEventDispatcher } from 'svelte'

	const _dispatch = createEventDispatcher()
    function click() {
        _dispatch('show',{num:number})
    }

	export let number;
</script>

<button on:click={ click }>{number}</button>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?