Chapter 89: Vue ‘computed’ Option

The computed option

This is the original, classic way to define derived / calculated values that automatically stay in sync with other reactive data. It is still extremely common in legacy codebases, in many job interviews (especially in India), in older tutorials, and in some advanced patterns — even in 2026.

Even if you personally write only <script setup> + Composition API today (which is the modern recommendation), you must understand the computed option properly because:

  • You will read/maintain old code written this way
  • Many job interviews still ask about it
  • Vue Devtools still shows computed properties in this format
  • Some plugins / mixins still rely on Options API

So let’s go through it step by step — like I’m pair-programming with you and explaining both the old world and why we mostly moved away from it.

1. What is the computed option? (Very simple mental model)

computed is an object where each key is the name of a derived value, and each value is a function that computes that value based on other reactive data (props, data, other computeds, etc.).

Vue automatically:

  • caches the result
  • re-computes only when one of the dependencies changes
  • makes the computed property reactive → any change triggers re-render where it’s used
JavaScript

After Vue processes this:

  • You can use {{ fullName }} or this.fullName in template / methods
  • It only re-calculates when firstName or lastName changes — not on every render
  • It behaves exactly like a property — no need to call it like a function

2. Real, Complete Example – User Profile Card with Computed Properties

vue

3. Important Rules & Gotchas (2026 Must-Know)

Rule / Gotcha Correct Behavior / Best Practice
Computed must be functions fullName() { … } — never fullName: value
Never mutate inside computed Computed should be pure — read-only, no side effects
Access via this this.fullName (not this.fullName()) — Vue calls it for you
Caching Only re-computes when dependencies change — very performant
Can depend on props, data, other computeds Yes — this.user.name, this.count, this.otherComputed all work
Can be used in template & JS {{ fullName }} or this.fullName in methods/watch
Arrow functions? Avoid — fullName: () => {} loses this binding → this becomes undefined
Still used in 2026? Yes — in legacy code, some plugins, interviews, but not in new <script setup> code

4. Modern Vue 3 – Why We Almost Never Use computed: { … } Anymore

In <script setup> (the 2026 standard):

vue

→ No computed object → No this → Just normal JavaScript constants → Same caching & reactivity, much cleaner & type-safe with TypeScript

Quick Summary Table – computed in 2026

Question Options API (legacy) Composition API (<script setup>) What you should do in new code
How to define computed? computed: { fullName() { … } } const fullName = computed(() => …) Composition API
Access in template {{ fullName }} {{ fullName }}
Access inside function this.fullName fullName.value
Still used in 2026? Yes — in legacy code, some plugins, interviews Almost never (except legacy migration) Avoid unless maintaining old code
TypeScript friendly? Poor Excellent (computed<ReturnType>(() => …)) Use Composition

Final 2026 Advice from Real Projects

  • In new projects → you should never write computed: { … } — just use computed(() => …) in <script setup>
  • When you see computed inside an object → it means Options API (legacy style)
  • Learn to read Options API — many jobs, open-source projects, tutorials still use it
  • Never teach beginners computed option as primary — start with computed(() => …) in <script setup>
  • If migrating old code → gradually convert computed → computed(() => …), etc.

Your mini homework:

  1. Create the UserCard component above in Options API
  2. Change user.age via button → see computed ageGroup & fullName update automatically
  3. Console.log this.fullName in mounted → see cached value
  4. Convert it to <script setup> → compare readability & TypeScript support

Any part confusing? Want to see:

  • Full Options API vs Composition API side-by-side project?
  • How computed looks in Vue Devtools?
  • Common bugs when using arrow functions in computed?
  • Real component written in both styles?

Just tell me — we’ll convert and compare together step by step 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *