How do components communicate? Props, emits, v-model, slots and provide/inject.
Parent to child: props
Declared with defineProps. Props are one-way: a child must not mutate them; it emits an event or keeps a local copy.
Child to parent: emits
Declared with defineEmits and fired with emit('save', payload). The parent listens with @save.
Two-way binding: v-model on components
defineModel() (Vue 3.4+) returns a ref linked to the parent's v-model; writing it emits update:modelValue for you. Named models (v-model:title) allow several.
Content: slots
The parent passes markup into <slot />. Named slots (<slot name="footer" />, used with <template #footer>) place content in several spots. Scoped slots pass data back to the parent's template (<slot :item="item" />, used as <template #default="{ item }">), which is how flexible list and table components are built.
Deep descendants: provide / inject
An ancestor provides a value; any descendant injects it, skipping the layers in between. Good for themes, form contexts and component libraries. Provide a ref to keep it reactive and a typed InjectionKey in TypeScript.
App-wide state
For state shared by unrelated parts of the app, use Pinia.
Example
<!-- QuantityInput.vue -->
<script setup>
const qty = defineModel({ type: Number, default: 1 });
const props = defineProps({ max: { type: Number, default: 10 } });
const emit = defineEmits(['limit']);
function inc() {
if (qty.value >= props.max) return emit('limit', props.max);
qty.value++;
}
</script>
<template>
<button @click="qty--" :disabled="qty <= 1">−</button>
<span>{{ qty }}</span>
<button @click="inc">+</button>
</template>
<!-- DataList.vue: a scoped slot -->
<script setup>
defineProps({ items: Array });
</script>
<template>
<ul>
<li v-for="item in items" :key="item.id"><slot :item="item">{{ item.name }}</slot></li>
</ul>
<footer><slot name="footer" /></footer>
</template>
<!-- Parent.vue -->
<script setup>
import { ref } from 'vue';
const quantity = ref(1);
const users = ref([{ id: 1, name: 'Asha', role: 'Admin' }]);
const notice = ref('');
const onLimit = max => { notice.value = `You can order at most ${max}`; };
</script>
<template>
<QuantityInput v-model="quantity" :max="5" @limit="onLimit" />
<p v-if="notice">{{ notice }}</p>
<DataList :items="users">
<template #default="{ item }"><strong>{{ item.name }}</strong> · {{ item.role }}</template>
<template #footer>{{ users.length }} users</template>
</DataList>
</template>Follow-up questions interviewers ask
Try answering each one before you open it.
What are fallthrough attributes?
Attributes and listeners passed to a component but not declared as props or emits (such as class, id, aria-* or @click) are added to the component's root element automatically. Set defineOptions({ inheritAttrs: false }) and bind v-bind="$attrs" (or useAttrs()) to place them on an inner element, for example the real <input> inside a wrapper.