Menu
CoddyTech

Event Payloads

Part of the Fundamentals section of Coddy's Vue journey. Lesson 37 of 55.

An emitted event can carry data. In this form, the child trims a draft and sends the accepted text. The parent handler receives the text as its argument.

const emit = defineEmits(['propose']);
function submit() {
  const text = draft.value.trim();
  if (!text) return;
  emit('propose', text);
}
// Parent: <ProposalForm @propose="saveProposal" />

The first argument to emit names the event; later arguments are its payload. Validate input before emitting when the contract promises valid text. The parent can store the latest value or append it to its own list without reaching into the child input.

challenge icon

Challenge

Medium

Create IdeaForm.vue with input#idea and button#send. On a nonblank suggestion, emit suggest with the trimmed text payload, then clear the child input. The parent listens and shows the latest suggestion in #result, initially No suggestion. Blank suggestions do not replace it.

Render the latest suggestion inside square brackets, for example [A new idea], so accepted leading and trailing whitespace is observable. The initial text is [No suggestion].

Try it yourself

<!doctype html><html><head><meta charset="UTF-8"><title>Vue practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals