Frontory

Open code, practice, and notes →

Download source with English comments · for reading

Vue · 17 provide and inject

Series contents · Technology map

Stage: Composables and Store Lifetimes · Editable example · Storage ID: vue-step-17

Goals and learning outcomes

Prerequisites

Unit 17 builds on the work and experiment records from Unit 16.

Key concepts and explanations

Passing dependencies

provide passes a value to a descendant tree. inject reads from the nearest provider, reducing intermediate prop passing.

Mutation location

Passing a reactive value still requires a separate decision about mutation responsibility. Providing a readonly value with a mutation function exposes the boundary.

Practice setup

First check the execution scope guidance on the example screen. The following is the preparation environment for extending the assignment in a separate project. Use .vue SFCs and script setup in a separate Vue Vite project. Record package versions and the lockfile, and run on the development server. The built-in Vue examples use the h function and are not a general-purpose SFC compiler.

The linked example is a starting point. It does not implement every task below. Complete work beyond its supported scope in a separate project.

Practice tasks

  1. In src/Workspace.vue, provide a readonly selection ref and select function with a Symbol key.
  2. Use inject in Detail.vue three levels down to display the title.
  3. Add a nested Workspace and check that each tree's selection is independent.

Variables to observe

VariableBaselineChangeExpected change / hypothesis
Provider placementOne shared providerTwo nested providersInner consumers read the selection from the nearest provider.

Predictions in this table are not measured results. Change one condition at a time and record why the actual result differs.

Completion criteria

Self-check

  1. How do you handle an injection with neither a default nor a provider?
  2. Why use a Symbol instead of a string key?

Official references

Linked example and execution scope

Example source · Runtime environments and limits

This is an experiment with actual Vue reactivity and render functions. Check SFC templates in an external compilation environment.

Source code and records are not overwritten automatically. Resetting an existing example also preserves your notes.

Next learning

Previous: Composable Lifetimes · Next: Shared Store Scope

Open code, practice, and notes →