Vue.js • Chapter 23 • Foundations to Advanced
Composition API with script setup
Each topic includes substantial explanation, ten focused examples, its own Vue code example, step-by-step reasoning, expected behavior, and practice.
23.1 script setup
script setup is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. In Chapter 23, trace the value or control flow from its source to the rendered interface so you can explain why Vue changes the screen.
For script setup in Chapter 23, inspect reactive inputs, component ownership, rendered output, edge cases, and what causes another update. Keep writable state ownership explicit, derive values when possible, and separate display calculations from network, DOM, storage, timer, or other external work.
This lesson connects script setup to composition api with script setup. Start with one working case, verify the expected output, then add one edge case and explain what Vue tracks, reuses, creates, removes, or updates.
Concept in plain language
script setup is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.
10 teaching examples
Example 1: Smallest useful case
Build the smallest analytics view that demonstrates script setup. Keep one input and one visible result, then explain reactive inputs, component ownership, rendered output, edge cases, and what causes another update. script setup is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 1 for Chapter 23, topic 1.
Example 2: Change one reactive value
Change one value involved in script setup inside the support form. Predict what Vue will update before running it, then compare the prediction with the rendered result.
Example 3: Parent-child comparison
Use script setup across two components in the course dashboard. Compare which component owns the writable data and which component only receives or presents it.
Example 4: Edge case
Add an empty, missing, invalid, delayed, or rapidly changing value to the profile editor. Handle the script setup edge case explicitly instead of leaving stale output.
Example 5: Accessibility review
Use script setup in the search panel while checking semantic HTML, labels, focus order, keyboard access, and understandable status feedback. script setup is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 5 for Chapter 23, topic 1.
Example 6: State ownership review
Remove duplicated state from the shopping cart. For script setup, derive values when possible and keep the writable source with the component or store that owns it.
Example 7: Slow-network scenario
Assume the lesson tracker has a slow request while using script setup. Decide what remains interactive, what shows pending feedback, and how stale responses are prevented.
Example 8: Refactoring exercise
Extract the script setup responsibility from a crowded booking form into a focused component or composable with a narrow API.
Example 9: Performance experiment
Measure the message panel before optimizing script setup. Check reactive work, repeated calculations, list size, component updates, and user-visible delay.
Example 10: Production review
Review script setup in the admin table for errors, security, localization, RTL, narrow screens, accessibility, and what monitoring should report. script setup is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 10 for Chapter 23, topic 1.
Vue code example
<script setup>
import { ref } from 'vue'
const topic = "script setup"
const active = ref(false)
</script>
<template>
<section><h2>{{ topic }}</h2><button :aria-pressed="active" @click="active=!active">Toggle state</button><p>{{ active ? 'Active' : 'Inactive' }}</p></section>
</template>Step-by-step code explanation
- Identify the Vue responsibility demonstrated by script setup.
- Read the reactive state, props, route/store input, or injected value before the template.
- Trace the event, dependency, watcher, lifecycle hook, or navigation action that changes the display.
- Test one normal path and one edge case, including cleanup when external work is involved.
- Confirm accessibility, mobile width, and RTL behavior for user-facing controls and translated text.
Expected behavior: A small Vue interface demonstrates script setup and updates according to the interaction or data in the example.
Practice exercise
Create a small Vue feature focused on script setup from Chapter 23. Predict the result before running it, test one edge case, and explain which reactive value, prop, event, route, store, or lifecycle step caused the update. Then check accessibility, narrow-screen width, and RTL behavior where relevant.
23.2 defineProps
defineProps is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. In Chapter 23, trace the value or control flow from its source to the rendered interface so you can explain why Vue changes the screen.
For defineProps in Chapter 23, inspect component ownership, data direction, event payloads, and the public component contract. Keep writable state ownership explicit, derive values when possible, and separate display calculations from network, DOM, storage, timer, or other external work.
This lesson connects defineProps to composition api with script setup. Start with one working case, verify the expected output, then add one edge case and explain what Vue tracks, reuses, creates, removes, or updates.
Concept in plain language
defineProps is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.
10 teaching examples
Example 1: Smallest useful case
Build the smallest profile editor that demonstrates defineProps. Keep one input and one visible result, then explain component ownership, data direction, event payloads, and the public component contract. defineProps is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 1 for Chapter 23, topic 2.
Example 2: Change one reactive value
Change one value involved in defineProps inside the search panel. Predict what Vue will update before running it, then compare the prediction with the rendered result.
Example 3: Parent-child comparison
Use defineProps across two components in the shopping cart. Compare which component owns the writable data and which component only receives or presents it.
Example 4: Edge case
Add an empty, missing, invalid, delayed, or rapidly changing value to the lesson tracker. Handle the defineProps edge case explicitly instead of leaving stale output.
Example 5: Accessibility review
Use defineProps in the booking form while checking semantic HTML, labels, focus order, keyboard access, and understandable status feedback. defineProps is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 5 for Chapter 23, topic 2.
Example 6: State ownership review
Remove duplicated state from the message panel. For defineProps, derive values when possible and keep the writable source with the component or store that owns it.
Example 7: Slow-network scenario
Assume the admin table has a slow request while using defineProps. Decide what remains interactive, what shows pending feedback, and how stale responses are prevented.
Example 8: Refactoring exercise
Extract the defineProps responsibility from a crowded photo gallery into a focused component or composable with a narrow API.
Example 9: Performance experiment
Measure the notification center before optimizing defineProps. Check reactive work, repeated calculations, list size, component updates, and user-visible delay.
Example 10: Production review
Review defineProps in the task board for errors, security, localization, RTL, narrow screens, accessibility, and what monitoring should report. defineProps is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 10 for Chapter 23, topic 2.
Vue code example
<script setup>
defineProps({ title: { type: String, required: true }, active: Boolean })
</script>
<template><article :class="{ active }"><h2>{{ title }}</h2></article></template>Step-by-step code explanation
- Identify the Vue responsibility demonstrated by defineProps.
- Read the reactive state, props, route/store input, or injected value before the template.
- Trace the event, dependency, watcher, lifecycle hook, or navigation action that changes the display.
- Test one normal path and one edge case, including cleanup when external work is involved.
- Confirm accessibility, mobile width, and RTL behavior for user-facing controls and translated text.
Expected behavior: A small Vue interface demonstrates defineProps and updates according to the interaction or data in the example.
Practice exercise
Create a small Vue feature focused on defineProps from Chapter 23. Predict the result before running it, test one edge case, and explain which reactive value, prop, event, route, store, or lifecycle step caused the update. Then check accessibility, narrow-screen width, and RTL behavior where relevant.
23.3 defineEmits
defineEmits is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. In Chapter 23, trace the value or control flow from its source to the rendered interface so you can explain why Vue changes the screen.
For defineEmits in Chapter 23, inspect component ownership, data direction, event payloads, and the public component contract. Keep writable state ownership explicit, derive values when possible, and separate display calculations from network, DOM, storage, timer, or other external work.
This lesson connects defineEmits to composition api with script setup. Start with one working case, verify the expected output, then add one edge case and explain what Vue tracks, reuses, creates, removes, or updates.
Concept in plain language
defineEmits is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.
10 teaching examples
Example 1: Smallest useful case
Build the smallest lesson tracker that demonstrates defineEmits. Keep one input and one visible result, then explain component ownership, data direction, event payloads, and the public component contract. defineEmits is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 1 for Chapter 23, topic 3.
Example 2: Change one reactive value
Change one value involved in defineEmits inside the booking form. Predict what Vue will update before running it, then compare the prediction with the rendered result.
Example 3: Parent-child comparison
Use defineEmits across two components in the message panel. Compare which component owns the writable data and which component only receives or presents it.
Example 4: Edge case
Add an empty, missing, invalid, delayed, or rapidly changing value to the admin table. Handle the defineEmits edge case explicitly instead of leaving stale output.
Example 5: Accessibility review
Use defineEmits in the photo gallery while checking semantic HTML, labels, focus order, keyboard access, and understandable status feedback. defineEmits is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 5 for Chapter 23, topic 3.
Example 6: State ownership review
Remove duplicated state from the notification center. For defineEmits, derive values when possible and keep the writable source with the component or store that owns it.
Example 7: Slow-network scenario
Assume the task board has a slow request while using defineEmits. Decide what remains interactive, what shows pending feedback, and how stale responses are prevented.
Example 8: Refactoring exercise
Extract the defineEmits responsibility from a crowded language selector into a focused component or composable with a narrow API.
Example 9: Performance experiment
Measure the quiz screen before optimizing defineEmits. Check reactive work, repeated calculations, list size, component updates, and user-visible delay.
Example 10: Production review
Review defineEmits in the analytics view for errors, security, localization, RTL, narrow screens, accessibility, and what monitoring should report. defineEmits is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 10 for Chapter 23, topic 3.
Vue code example
<script setup>
const emit = defineEmits(['select'])
const choose = () => emit('select', { id: 7, label: 'Selected item' })
</script>
<template><button @click="choose">Select</button></template>Step-by-step code explanation
- Identify the Vue responsibility demonstrated by defineEmits.
- Read the reactive state, props, route/store input, or injected value before the template.
- Trace the event, dependency, watcher, lifecycle hook, or navigation action that changes the display.
- Test one normal path and one edge case, including cleanup when external work is involved.
- Confirm accessibility, mobile width, and RTL behavior for user-facing controls and translated text.
Expected behavior: A small Vue interface demonstrates defineEmits and updates according to the interaction or data in the example.
Practice exercise
Create a small Vue feature focused on defineEmits from Chapter 23. Predict the result before running it, test one edge case, and explain which reactive value, prop, event, route, store, or lifecycle step caused the update. Then check accessibility, narrow-screen width, and RTL behavior where relevant.
23.4 defineExpose
defineExpose is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. In Chapter 23, trace the value or control flow from its source to the rendered interface so you can explain why Vue changes the screen.
For defineExpose in Chapter 23, inspect reactive inputs, component ownership, rendered output, edge cases, and what causes another update. Keep writable state ownership explicit, derive values when possible, and separate display calculations from network, DOM, storage, timer, or other external work.
This lesson connects defineExpose to composition api with script setup. Start with one working case, verify the expected output, then add one edge case and explain what Vue tracks, reuses, creates, removes, or updates.
Concept in plain language
defineExpose is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.
10 teaching examples
Example 1: Smallest useful case
Build the smallest admin table that demonstrates defineExpose. Keep one input and one visible result, then explain reactive inputs, component ownership, rendered output, edge cases, and what causes another update. defineExpose is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 1 for Chapter 23, topic 4.
Example 2: Change one reactive value
Change one value involved in defineExpose inside the photo gallery. Predict what Vue will update before running it, then compare the prediction with the rendered result.
Example 3: Parent-child comparison
Use defineExpose across two components in the notification center. Compare which component owns the writable data and which component only receives or presents it.
Example 4: Edge case
Add an empty, missing, invalid, delayed, or rapidly changing value to the task board. Handle the defineExpose edge case explicitly instead of leaving stale output.
Example 5: Accessibility review
Use defineExpose in the language selector while checking semantic HTML, labels, focus order, keyboard access, and understandable status feedback. defineExpose is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 5 for Chapter 23, topic 4.
Example 6: State ownership review
Remove duplicated state from the quiz screen. For defineExpose, derive values when possible and keep the writable source with the component or store that owns it.
Example 7: Slow-network scenario
Assume the analytics view has a slow request while using defineExpose. Decide what remains interactive, what shows pending feedback, and how stale responses are prevented.
Example 8: Refactoring exercise
Extract the defineExpose responsibility from a crowded support form into a focused component or composable with a narrow API.
Example 9: Performance experiment
Measure the course dashboard before optimizing defineExpose. Check reactive work, repeated calculations, list size, component updates, and user-visible delay.
Example 10: Production review
Review defineExpose in the profile editor for errors, security, localization, RTL, narrow screens, accessibility, and what monitoring should report. defineExpose is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 10 for Chapter 23, topic 4.
Vue code example
<script setup>
import { ref } from 'vue'
const topic = "defineExpose"
const active = ref(false)
</script>
<template>
<section><h2>{{ topic }}</h2><button :aria-pressed="active" @click="active=!active">Toggle state</button><p>{{ active ? 'Active' : 'Inactive' }}</p></section>
</template>Step-by-step code explanation
- Identify the Vue responsibility demonstrated by defineExpose.
- Read the reactive state, props, route/store input, or injected value before the template.
- Trace the event, dependency, watcher, lifecycle hook, or navigation action that changes the display.
- Test one normal path and one edge case, including cleanup when external work is involved.
- Confirm accessibility, mobile width, and RTL behavior for user-facing controls and translated text.
Expected behavior: A small Vue interface demonstrates defineExpose and updates according to the interaction or data in the example.
Practice exercise
Create a small Vue feature focused on defineExpose from Chapter 23. Predict the result before running it, test one edge case, and explain which reactive value, prop, event, route, store, or lifecycle step caused the update. Then check accessibility, narrow-screen width, and RTL behavior where relevant.
23.5 Top-Level Bindings in Templates
Top-Level Bindings in Templates is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. In Chapter 23, trace the value or control flow from its source to the rendered interface so you can explain why Vue changes the screen.
For Top-Level Bindings in Templates in Chapter 23, inspect reactive inputs, component ownership, rendered output, edge cases, and what causes another update. Keep writable state ownership explicit, derive values when possible, and separate display calculations from network, DOM, storage, timer, or other external work.
This lesson connects Top-Level Bindings in Templates to composition api with script setup. Start with one working case, verify the expected output, then add one edge case and explain what Vue tracks, reuses, creates, removes, or updates.
Concept in plain language
Top-Level Bindings in Templates is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.
10 teaching examples
Example 1: Smallest useful case
Build the smallest task board that demonstrates Top-Level Bindings in Templates. Keep one input and one visible result, then explain reactive inputs, component ownership, rendered output, edge cases, and what causes another update. Top-Level Bindings in Templates is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 1 for Chapter 23, topic 5.
Example 2: Change one reactive value
Change one value involved in Top-Level Bindings in Templates inside the language selector. Predict what Vue will update before running it, then compare the prediction with the rendered result.
Example 3: Parent-child comparison
Use Top-Level Bindings in Templates across two components in the quiz screen. Compare which component owns the writable data and which component only receives or presents it.
Example 4: Edge case
Add an empty, missing, invalid, delayed, or rapidly changing value to the analytics view. Handle the Top-Level Bindings in Templates edge case explicitly instead of leaving stale output.
Example 5: Accessibility review
Use Top-Level Bindings in Templates in the support form while checking semantic HTML, labels, focus order, keyboard access, and understandable status feedback. Top-Level Bindings in Templates is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 5 for Chapter 23, topic 5.
Example 6: State ownership review
Remove duplicated state from the course dashboard. For Top-Level Bindings in Templates, derive values when possible and keep the writable source with the component or store that owns it.
Example 7: Slow-network scenario
Assume the profile editor has a slow request while using Top-Level Bindings in Templates. Decide what remains interactive, what shows pending feedback, and how stale responses are prevented.
Example 8: Refactoring exercise
Extract the Top-Level Bindings in Templates responsibility from a crowded search panel into a focused component or composable with a narrow API.
Example 9: Performance experiment
Measure the shopping cart before optimizing Top-Level Bindings in Templates. Check reactive work, repeated calculations, list size, component updates, and user-visible delay.
Example 10: Production review
Review Top-Level Bindings in Templates in the lesson tracker for errors, security, localization, RTL, narrow screens, accessibility, and what monitoring should report. Top-Level Bindings in Templates is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 10 for Chapter 23, topic 5.
Vue code example
<script setup>
import { ref } from 'vue'
const topic = "Top-Level Bindings in Templates"
const active = ref(false)
</script>
<template>
<section><h2>{{ topic }}</h2><button :aria-pressed="active" @click="active=!active">Toggle state</button><p>{{ active ? 'Active' : 'Inactive' }}</p></section>
</template>Step-by-step code explanation
- Identify the Vue responsibility demonstrated by Top-Level Bindings in Templates.
- Read the reactive state, props, route/store input, or injected value before the template.
- Trace the event, dependency, watcher, lifecycle hook, or navigation action that changes the display.
- Test one normal path and one edge case, including cleanup when external work is involved.
- Confirm accessibility, mobile width, and RTL behavior for user-facing controls and translated text.
Expected behavior: A small Vue interface demonstrates Top-Level Bindings in Templates and updates according to the interaction or data in the example.
Practice exercise
Create a small Vue feature focused on Top-Level Bindings in Templates from Chapter 23. Predict the result before running it, test one edge case, and explain which reactive value, prop, event, route, store, or lifecycle step caused the update. Then check accessibility, narrow-screen width, and RTL behavior where relevant.
Chapter 23 review — 10 questions and answers
1. What is the purpose of script setup?
Answer: script setup is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.
2. What should you inspect when script setup behaves unexpectedly?
Answer: Inspect reactive inputs, component ownership, rendered output, edge cases, and what causes another update. Reduce the feature to a small component and trace reactive input through the rendered result.
3. What is the purpose of defineProps?
Answer: defineProps is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.
4. What should you inspect when defineProps behaves unexpectedly?
Answer: Inspect component ownership, data direction, event payloads, and the public component contract. Reduce the feature to a small component and trace reactive input through the rendered result.
5. What is the purpose of defineEmits?
Answer: defineEmits is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.
6. What should you inspect when defineEmits behaves unexpectedly?
Answer: Inspect component ownership, data direction, event payloads, and the public component contract. Reduce the feature to a small component and trace reactive input through the rendered result.
7. What is the purpose of defineExpose?
Answer: defineExpose is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.
8. What should you inspect when defineExpose behaves unexpectedly?
Answer: Inspect reactive inputs, component ownership, rendered output, edge cases, and what causes another update. Reduce the feature to a small component and trace reactive input through the rendered result.
9. What is the purpose of Top-Level Bindings in Templates?
Answer: Top-Level Bindings in Templates is a focused part of Vue application design in Chapter 23. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.
10. What should you inspect when Top-Level Bindings in Templates behaves unexpectedly?
Answer: Inspect reactive inputs, component ownership, rendered output, edge cases, and what causes another update. Reduce the feature to a small component and trace reactive input through the rendered result.