Frontory

Open code, practice, and notes →

Download source with English comments · for reading

Vue · 16 Composable Lifetimes

Series contents · Technology map

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

Goals and learning outcomes

Prerequisites

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

Key concepts and explanations

Grouping logic

A Composable is a reusable function that uses the Composition API. Specify whether each call creates independent state or shares it.

Cleanup responsibility

Lifecycle Hooks registered inside a component are tied to that instance. Remove external listeners at the same termination point.

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. Group a ref, resize listener, and onUnmounted in src/composables/useWindowSize.js.
  2. Call it in two panels and display the size.
  3. Remove panels one by one and check the listener count and remaining display.

Variables to observe

VariableBaselineChangeExpected change / hypothesis
Consumer count1 consumer2 consumersAn independent Composable has two listeners, which are removed one by one.

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. Is state independent for each call when using a module-scope ref?
  2. Why is registering onMounted inside setTimeout a problem?

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: Template Refs and nextTick · Next: provide and inject

Open code, practice, and notes →