A. Event name 'update' must be declared in emits array
B. Cannot emit events from methods
C. Should use $emit instead of emit
D. Payload 123 must be a string
Solution
Step 1: Check method call syntax
The Vue instance method to emit events is $emit, not emit.
Step 2: Confirm event declaration
While declaring events in emits is recommended, not declaring does not cause this error.
Final Answer:
Should use $emit instead of emit -> Option C
Quick Check:
Use this.$emit to emit events = B [OK]
Hint: Always use this.$emit to send events [OK]
Common Mistakes:
Forgetting the $ in $emit
Thinking emits array is mandatory for emitting
Assuming payload must be string
5. You want a child component to send two different events, save and cancel, each with a payload. How should you define and emit these events properly in Vue 3 Composition API <script setup>?
hard
A. const emit = defineEmits({ save: null, cancel: null })
function onSave() { emit('save', data) }
function onCancel() { emit('cancel', reason) }
B. defineEmits(['save', 'cancel'])
function onSave() { emit('save', data) }
function onCancel() { emit('cancel', reason) }
C. defineEmits(['save', 'cancel'])
const emit = defineEmits(['save', 'cancel'])
function onSave() { emit('save', data) }
function onCancel() { emit('cancel', reason) }
D. const emit = defineEmits(['save', 'cancel'])
function onSave() { this.$emit('save', data) }
function onCancel() { this.$emit('cancel', reason) }
Solution
Step 1: Understand defineEmits usage in <script setup>
In Vue 3 Composition API with <script setup>, defineEmits returns an emit function. It can accept an object to define event names and validators or an array of event names.
Step 2: Identify correct syntax for multiple events
const emit = defineEmits({ save: null, cancel: null })
function onSave() { emit('save', data) }
function onCancel() { emit('cancel', reason) } uses defineEmits({ save: null, cancel: null }) which is valid and recommended for typing events. Then emits events with emit('save', data) and emit('cancel', reason).
Step 3: Check incorrect options
defineEmits(['save', 'cancel'])
function onSave() { emit('save', data) }
function onCancel() { emit('cancel', reason) } calls defineEmits without assigning the result to emit, so emit is undefined. defineEmits(['save', 'cancel'])
const emit = defineEmits(['save', 'cancel'])
function onSave() { emit('save', data) }
function onCancel() { emit('cancel', reason) } calls defineEmits twice which is invalid. const emit = defineEmits(['save', 'cancel'])
function onSave() { this.$emit('save', data) }
function onCancel() { this.$emit('cancel', reason) } uses this.$emit which is not available in <script setup>.
Final Answer:
const emit = defineEmits({ save: null, cancel: null }) with emit calls -> Option A
Quick Check:
Use defineEmits object + emit() in script setup = A [OK]
Hint: Use defineEmits object and emit() in script setup [OK]
Common Mistakes:
Calling defineEmits without assigning to emit
Calling defineEmits twice
Using this.$emit inside script setup
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.