← Vue.js learning pathVue.js · Module 6 of 10
Computed, watchers and lifecycle
Derive values, react to changes and run code at the right moments.
📖 Key ideas
- computed(() => ...) caches a derived value.
- watch(source, cb) runs code when data changes.
- onMounted runs after the component is added to the page.
- onUnmounted cleans up timers and listeners.
- 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