Vue.js & Composition API (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)
What are `defineProps` and `defineEmits` in `<script setup>`?
junior LevelCode ExampleComponents
In Simple Words: Compiler macros (no need to import them!) used to declare component API contracts:
vue
<script setup lang="ts">
// Typed Props with defaults
const props = withDefaults(defineProps<{
title: string;
count?: number;
}>(), {
count: 0
});
// Typed Emits
const emit = defineEmits<{
(e: 'change', id: number): void;
(e: 'close'): void;
}>();
function handleClick() {
emit('change', 42);
}
</script>