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?

【vue/Nuxt.js】composition APIを使用した子から親へのデータ受け渡しの書き方

Posted at

composition APIを使用して子コンポーネントのデータ(イベント)を親コンポーネントに渡す方法についてメモ:writing_hand:

defineEmitsで定義する

composition APIでは$emitの役割を持つ変数を作ってdefineEmitsでカスタムイベントを定義します。

子コンポーネント側

<script setup>
const emit = defineEmits(['hogeEvent']); //emit以外でもok
const hoge = () =>{
    emit('hogeEvent');
}
</script>
<template>
    <button type="button" @click="hoge">emitテスト</button>
</template>

親コンポーネント側

親コンポーネント側は定義したカスタムイベント名に処理を書くだけでok。

<script setup>
const customEvent = () =>{
  console.log('test')
}
</script>
<template>
  <ChiledComponents @hogeEvent="customEvent"></ChiledComponents>
</template>

参考

【Vue3 props/emit】コンポーネント間通信
defineProps() & defineEmits()

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?