🎓 EASYTUTORGUIDE

Practical tutorials, tools, courses, digital skills, and business promotion.

Translate this lesson:
Arabic and Persian use right-to-left layout. Vue code stays left-to-right.

Vue.js • Chapter 33 • Foundations to Advanced

Custom Directives

Each topic includes substantial explanation, ten focused examples, its own Vue code example, step-by-step reasoning, expected behavior, and practice.

5 topics50 teaching examplesCode example per topic10 Q&A
Estimated reading time0% read

33.1 Directive Basics

Directive Basics is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. In Chapter 33, trace the value or control flow from its source to the rendered interface so you can explain why Vue changes the screen.

For Directive Basics in Chapter 33, 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 Directive Basics to custom directives. 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

Directive Basics is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.

10 teaching examples

  1. Example 1: Smallest useful case

    Build the smallest shopping cart that demonstrates Directive Basics. Keep one input and one visible result, then explain reactive inputs, component ownership, rendered output, edge cases, and what causes another update. Directive Basics is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 1 for Chapter 33, topic 1.

  2. Example 2: Change one reactive value

    Change one value involved in Directive Basics inside the lesson tracker. Predict what Vue will update before running it, then compare the prediction with the rendered result.

  3. Example 3: Parent-child comparison

    Use Directive Basics across two components in the booking form. Compare which component owns the writable data and which component only receives or presents it.

  4. Example 4: Edge case

    Add an empty, missing, invalid, delayed, or rapidly changing value to the message panel. Handle the Directive Basics edge case explicitly instead of leaving stale output.

  5. Example 5: Accessibility review

    Use Directive Basics in the admin table while checking semantic HTML, labels, focus order, keyboard access, and understandable status feedback. Directive Basics is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 5 for Chapter 33, topic 1.

  6. Example 6: State ownership review

    Remove duplicated state from the photo gallery. For Directive Basics, derive values when possible and keep the writable source with the component or store that owns it.

  7. Example 7: Slow-network scenario

    Assume the notification center has a slow request while using Directive Basics. Decide what remains interactive, what shows pending feedback, and how stale responses are prevented.

  8. Example 8: Refactoring exercise

    Extract the Directive Basics responsibility from a crowded task board into a focused component or composable with a narrow API.

  9. Example 9: Performance experiment

    Measure the language selector before optimizing Directive Basics. Check reactive work, repeated calculations, list size, component updates, and user-visible delay.

  10. Example 10: Production review

    Review Directive Basics in the quiz screen for errors, security, localization, RTL, narrow screens, accessibility, and what monitoring should report. Directive Basics is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 10 for Chapter 33, topic 1.

Vue code example

export const vFocus = { mounted(el){ el.focus() } }
// template: <input v-focus>

Step-by-step code explanation

  1. Identify the Vue responsibility demonstrated by Directive Basics.
  2. Read the reactive state, props, route/store input, or injected value before the template.
  3. Trace the event, dependency, watcher, lifecycle hook, or navigation action that changes the display.
  4. Test one normal path and one edge case, including cleanup when external work is involved.
  5. Confirm accessibility, mobile width, and RTL behavior for user-facing controls and translated text.

Expected behavior: A small Vue interface demonstrates Directive Basics and updates according to the interaction or data in the example.

Practice exercise

Create a small Vue feature focused on Directive Basics from Chapter 33. 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.

33.2 Directive Hooks

Directive Hooks is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. In Chapter 33, trace the value or control flow from its source to the rendered interface so you can explain why Vue changes the screen.

For Directive Hooks in Chapter 33, 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 Directive Hooks to custom directives. 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

Directive Hooks is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.

10 teaching examples

  1. Example 1: Smallest useful case

    Build the smallest message panel that demonstrates Directive Hooks. Keep one input and one visible result, then explain reactive inputs, component ownership, rendered output, edge cases, and what causes another update. Directive Hooks is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 1 for Chapter 33, topic 2.

  2. Example 2: Change one reactive value

    Change one value involved in Directive Hooks inside the admin table. Predict what Vue will update before running it, then compare the prediction with the rendered result.

  3. Example 3: Parent-child comparison

    Use Directive Hooks across two components in the photo gallery. Compare which component owns the writable data and which component only receives or presents it.

  4. Example 4: Edge case

    Add an empty, missing, invalid, delayed, or rapidly changing value to the notification center. Handle the Directive Hooks edge case explicitly instead of leaving stale output.

  5. Example 5: Accessibility review

    Use Directive Hooks in the task board while checking semantic HTML, labels, focus order, keyboard access, and understandable status feedback. Directive Hooks is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 5 for Chapter 33, topic 2.

  6. Example 6: State ownership review

    Remove duplicated state from the language selector. For Directive Hooks, derive values when possible and keep the writable source with the component or store that owns it.

  7. Example 7: Slow-network scenario

    Assume the quiz screen has a slow request while using Directive Hooks. Decide what remains interactive, what shows pending feedback, and how stale responses are prevented.

  8. Example 8: Refactoring exercise

    Extract the Directive Hooks responsibility from a crowded analytics view into a focused component or composable with a narrow API.

  9. Example 9: Performance experiment

    Measure the support form before optimizing Directive Hooks. Check reactive work, repeated calculations, list size, component updates, and user-visible delay.

  10. Example 10: Production review

    Review Directive Hooks in the course dashboard for errors, security, localization, RTL, narrow screens, accessibility, and what monitoring should report. Directive Hooks is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 10 for Chapter 33, topic 2.

Vue code example

export const vFocus = { mounted(el){ el.focus() } }
// template: <input v-focus>

Step-by-step code explanation

  1. Identify the Vue responsibility demonstrated by Directive Hooks.
  2. Read the reactive state, props, route/store input, or injected value before the template.
  3. Trace the event, dependency, watcher, lifecycle hook, or navigation action that changes the display.
  4. Test one normal path and one edge case, including cleanup when external work is involved.
  5. Confirm accessibility, mobile width, and RTL behavior for user-facing controls and translated text.

Expected behavior: A small Vue interface demonstrates Directive Hooks and updates according to the interaction or data in the example.

Practice exercise

Create a small Vue feature focused on Directive Hooks from Chapter 33. 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.

33.3 Binding Values

Binding Values is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. In Chapter 33, trace the value or control flow from its source to the rendered interface so you can explain why Vue changes the screen.

For Binding Values in Chapter 33, 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 Binding Values to custom directives. 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

Binding Values is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.

10 teaching examples

  1. Example 1: Smallest useful case

    Build the smallest notification center that demonstrates Binding Values. Keep one input and one visible result, then explain reactive inputs, component ownership, rendered output, edge cases, and what causes another update. Binding Values is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 1 for Chapter 33, topic 3.

  2. Example 2: Change one reactive value

    Change one value involved in Binding Values inside the task board. Predict what Vue will update before running it, then compare the prediction with the rendered result.

  3. Example 3: Parent-child comparison

    Use Binding Values across two components in the language selector. Compare which component owns the writable data and which component only receives or presents it.

  4. Example 4: Edge case

    Add an empty, missing, invalid, delayed, or rapidly changing value to the quiz screen. Handle the Binding Values edge case explicitly instead of leaving stale output.

  5. Example 5: Accessibility review

    Use Binding Values in the analytics view while checking semantic HTML, labels, focus order, keyboard access, and understandable status feedback. Binding Values is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 5 for Chapter 33, topic 3.

  6. Example 6: State ownership review

    Remove duplicated state from the support form. For Binding Values, derive values when possible and keep the writable source with the component or store that owns it.

  7. Example 7: Slow-network scenario

    Assume the course dashboard has a slow request while using Binding Values. Decide what remains interactive, what shows pending feedback, and how stale responses are prevented.

  8. Example 8: Refactoring exercise

    Extract the Binding Values responsibility from a crowded profile editor into a focused component or composable with a narrow API.

  9. Example 9: Performance experiment

    Measure the search panel before optimizing Binding Values. Check reactive work, repeated calculations, list size, component updates, and user-visible delay.

  10. Example 10: Production review

    Review Binding Values in the shopping cart for errors, security, localization, RTL, narrow screens, accessibility, and what monitoring should report. Binding Values is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 10 for Chapter 33, topic 3.

Vue code example

<script setup>
import { ref } from 'vue'
const topic = "Binding Values"
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

  1. Identify the Vue responsibility demonstrated by Binding Values.
  2. Read the reactive state, props, route/store input, or injected value before the template.
  3. Trace the event, dependency, watcher, lifecycle hook, or navigation action that changes the display.
  4. Test one normal path and one edge case, including cleanup when external work is involved.
  5. Confirm accessibility, mobile width, and RTL behavior for user-facing controls and translated text.

Expected behavior: A small Vue interface demonstrates Binding Values and updates according to the interaction or data in the example.

Practice exercise

Create a small Vue feature focused on Binding Values from Chapter 33. 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.

33.4 Directive Arguments

Directive Arguments is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. In Chapter 33, trace the value or control flow from its source to the rendered interface so you can explain why Vue changes the screen.

For Directive Arguments in Chapter 33, 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 Directive Arguments to custom directives. 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

Directive Arguments is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.

10 teaching examples

  1. Example 1: Smallest useful case

    Build the smallest quiz screen that demonstrates Directive Arguments. Keep one input and one visible result, then explain reactive inputs, component ownership, rendered output, edge cases, and what causes another update. Directive Arguments is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 1 for Chapter 33, topic 4.

  2. Example 2: Change one reactive value

    Change one value involved in Directive Arguments inside the analytics view. Predict what Vue will update before running it, then compare the prediction with the rendered result.

  3. Example 3: Parent-child comparison

    Use Directive Arguments across two components in the support form. Compare which component owns the writable data and which component only receives or presents it.

  4. Example 4: Edge case

    Add an empty, missing, invalid, delayed, or rapidly changing value to the course dashboard. Handle the Directive Arguments edge case explicitly instead of leaving stale output.

  5. Example 5: Accessibility review

    Use Directive Arguments in the profile editor while checking semantic HTML, labels, focus order, keyboard access, and understandable status feedback. Directive Arguments is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 5 for Chapter 33, topic 4.

  6. Example 6: State ownership review

    Remove duplicated state from the search panel. For Directive Arguments, derive values when possible and keep the writable source with the component or store that owns it.

  7. Example 7: Slow-network scenario

    Assume the shopping cart has a slow request while using Directive Arguments. Decide what remains interactive, what shows pending feedback, and how stale responses are prevented.

  8. Example 8: Refactoring exercise

    Extract the Directive Arguments responsibility from a crowded lesson tracker into a focused component or composable with a narrow API.

  9. Example 9: Performance experiment

    Measure the booking form before optimizing Directive Arguments. Check reactive work, repeated calculations, list size, component updates, and user-visible delay.

  10. Example 10: Production review

    Review Directive Arguments in the message panel for errors, security, localization, RTL, narrow screens, accessibility, and what monitoring should report. Directive Arguments is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 10 for Chapter 33, topic 4.

Vue code example

export const vFocus = { mounted(el){ el.focus() } }
// template: <input v-focus>

Step-by-step code explanation

  1. Identify the Vue responsibility demonstrated by Directive Arguments.
  2. Read the reactive state, props, route/store input, or injected value before the template.
  3. Trace the event, dependency, watcher, lifecycle hook, or navigation action that changes the display.
  4. Test one normal path and one edge case, including cleanup when external work is involved.
  5. Confirm accessibility, mobile width, and RTL behavior for user-facing controls and translated text.

Expected behavior: A small Vue interface demonstrates Directive Arguments and updates according to the interaction or data in the example.

Practice exercise

Create a small Vue feature focused on Directive Arguments from Chapter 33. 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.

33.5 When a Component Is Better

When a Component Is Better is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. In Chapter 33, trace the value or control flow from its source to the rendered interface so you can explain why Vue changes the screen.

For When a Component Is Better in Chapter 33, 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 When a Component Is Better to custom directives. 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

When a Component Is Better is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.

10 teaching examples

  1. Example 1: Smallest useful case

    Build the smallest course dashboard that demonstrates When a Component Is Better. Keep one input and one visible result, then explain reactive inputs, component ownership, rendered output, edge cases, and what causes another update. When a Component Is Better is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 1 for Chapter 33, topic 5.

  2. Example 2: Change one reactive value

    Change one value involved in When a Component Is Better inside the profile editor. Predict what Vue will update before running it, then compare the prediction with the rendered result.

  3. Example 3: Parent-child comparison

    Use When a Component Is Better across two components in the search panel. Compare which component owns the writable data and which component only receives or presents it.

  4. Example 4: Edge case

    Add an empty, missing, invalid, delayed, or rapidly changing value to the shopping cart. Handle the When a Component Is Better edge case explicitly instead of leaving stale output.

  5. Example 5: Accessibility review

    Use When a Component Is Better in the lesson tracker while checking semantic HTML, labels, focus order, keyboard access, and understandable status feedback. When a Component Is Better is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 5 for Chapter 33, topic 5.

  6. Example 6: State ownership review

    Remove duplicated state from the booking form. For When a Component Is Better, derive values when possible and keep the writable source with the component or store that owns it.

  7. Example 7: Slow-network scenario

    Assume the message panel has a slow request while using When a Component Is Better. Decide what remains interactive, what shows pending feedback, and how stale responses are prevented.

  8. Example 8: Refactoring exercise

    Extract the When a Component Is Better responsibility from a crowded admin table into a focused component or composable with a narrow API.

  9. Example 9: Performance experiment

    Measure the photo gallery before optimizing When a Component Is Better. Check reactive work, repeated calculations, list size, component updates, and user-visible delay.

  10. Example 10: Production review

    Review When a Component Is Better in the notification center for errors, security, localization, RTL, narrow screens, accessibility, and what monitoring should report. When a Component Is Better is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements. This is example 10 for Chapter 33, topic 5.

Vue code example

<script setup>
import { ref } from 'vue'
const topic = "When a Component Is Better"
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

  1. Identify the Vue responsibility demonstrated by When a Component Is Better.
  2. Read the reactive state, props, route/store input, or injected value before the template.
  3. Trace the event, dependency, watcher, lifecycle hook, or navigation action that changes the display.
  4. Test one normal path and one edge case, including cleanup when external work is involved.
  5. Confirm accessibility, mobile width, and RTL behavior for user-facing controls and translated text.

Expected behavior: A small Vue interface demonstrates When a Component Is Better and updates according to the interaction or data in the example.

Practice exercise

Create a small Vue feature focused on When a Component Is Better from Chapter 33. 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 33 review — 10 questions and answers

1. What is the purpose of Directive Basics?

Answer: Directive Basics is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.

2. What should you inspect when Directive Basics 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 Directive Hooks?

Answer: Directive Hooks is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.

4. What should you inspect when Directive Hooks 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.

5. What is the purpose of Binding Values?

Answer: Binding Values is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.

6. What should you inspect when Binding Values 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.

7. What is the purpose of Directive Arguments?

Answer: Directive Arguments is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.

8. What should you inspect when Directive Arguments 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 When a Component Is Better?

Answer: When a Component Is Better is a focused part of Vue application design in Chapter 33. Study its inputs, reactive dependencies, component ownership, rendered result, edge cases, and cleanup requirements.

10. What should you inspect when When a Component Is Better 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.