Lesson 3 of 9 · 22m
Component Contracts: props, emits, slots, and v-model
Design Vue components whose interfaces stay stable under feature pressure — explicit props, typed emits, slots for composition, and disciplined v-model.
Most large Vue codebases do not fail because reactivity is hard. They fail because component boundaries are vague: props do three jobs, events are inconsistent, slots are bolted on later, and v-model gets abused until nobody knows who owns the value. Treat a component as an API surface and Vue becomes pleasantly boring.
Props are inputs; validate them like an API
<script setup lang="ts">
const props = defineProps<{
userId: string;
expanded?: boolean;
density?: "compact" | "comfortable";
}>();
const emit = defineEmits<{
select: [userId: string];
close: [];
}>();
</script>Keep prop names stable and explicit. Avoid mega-props like options unless you are deliberately shipping a configurable widget. If a parent passes twelve unrelated values, the child is probably doing too much or the parent is trying to remote-control internals.
Emit facts upward, not commands
<button type="button" @click="emit('select', props.userId)">Select</button>Events should describe what happened in the child: select, close, submit, remove. They should not instruct the parent to mutate implementation details. The child owns interaction; the parent owns policy. When this reverses, components become impossible to reuse because every consumer must reimplement their internals.
Slots beat configuration props for layout
<UserCard>
<template #actions>
<button type="button" @click="approve(user)">Approve</button>
</template>
</UserCard>Slots are the composition mechanism Vue gives you. If a component needs customizable regions, prefer named slots over props like renderHeader or boolean switches for every variation. Slots keep markup declarative and let the parent compose content without the child knowing its details.
v-model is a contract, not a convenience hack
<script setup lang="ts">
const model = defineModel<string>({ required: true });
</script>
<template>
<input v-model="model" autocomplete="email" />
</template>Vue 3.4's defineModel is the right tool for a component whose whole job is editing a value — inputs, date pickers, token fields. Multiple models are supported (defineModel('visible')), but restraint matters: two-way binding hides mutation direction. Use it where editing is intrinsic, not as a shortcut around explicit events.
Field notes
- Use kebab-case in templates and camelCase in script definitions; inconsistency here makes refactoring painful.
inheritAttrs: falseplusv-bind="$attrs"is how you build wrapper inputs without losing accessibility attributes.- Every interactive child should be usable by keyboard and screen reader before you add styling; composability doesn't excuse broken semantics.
- If a component needs both controlled and uncontrolled modes, pick one primary pattern. Supporting both doubles edge cases.
Was this lesson helpful?