← Vue.js learning path
Vue.js · Module 6 of 10

Computed, watchers and lifecycle

Derive values, react to changes and run code at the right moments.

📖 Key ideas

  1. computed(() => ...) caches a derived value.
  2. watch(source, cb) runs code when data changes.
  3. onMounted runs after the component is added to the page.
  4. onUnmounted cleans up timers and listeners.
  5. Prefer computed over watch for derived data.
💡 Real-life example: computed is a calculator that remembers the answer until the numbers change.
computedCached derived value
watchRuns code when a source changes
onMountedHook after mounting
onUnmountedHook for cleanup