Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Emitting Events from Components | Section
Vue.js Fundamentals and App Development

bookEmitting Events from Components

メニューを表示するにはスワイプしてください

Components can send data back to their parent by emitting events. This allows child components to communicate when something happens, such as a button click.

To emit an event, use defineEmits() inside the component.

<!-- ButtonComponent.vue -->
<script setup>
const emit = defineEmits(["clickEvent"]);

function handleClick() {
  emit("clickEvent");
}
</script>

<template>
  <button @click="handleClick">Click</button>
</template>

The component emits a custom event called "clickEvent".

In the parent component, you listen to this event.

<script setup>
import ButtonComponent from "./components/ButtonComponent.vue";

function handleEvent() {
  console.log("Event received");
}
</script>

<template>
  <ButtonComponent @clickEvent="handleEvent" />
</template>

When the button is clicked, the event is emitted and handled in the parent.

Emitting events allows components to communicate and keep logic organized.

question mark

Which statement best describes how a child component emits an event to its parent in Vue.js?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  12

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  12
some-alt