A computed property calculates a value from other data and is cached until its dependencies change.
Use computed properties instead of long expressions in templates.
Values derived from other data.
✍️ 3 exercises + code challenge in this lessonA computed property calculates a value from other data and is cached until its dependencies change.
Use computed properties instead of long expressions in templates.
Mastering Vue Computed Properties helps you write clean and reliable Vue.js code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
<script src="https://unpkg.com/vue@3.5.12/dist/vue.global.prod.js"></script>
<div id="app">
<p v-for="item in cart" :key="item.name">{{ item.name }}: Rs {{ item.price }} × {{ item.qty }}</p>
<h3>Total: Rs {{ total }}</h3>
<p>{{ itemCount }} items</p>
</div>
<script>
Vue.createApp({
data() {
return { cart: [ { name: "Pen", price: 50, qty: 3 }, { name: "Notebook", price: 250, qty: 2 } ] };
},
computed: {
total() { return this.cart.reduce((sum, i) => sum + i.price * i.qty, 0); },
itemCount() { return this.cart.reduce((sum, i) => sum + i.qty, 0); }
}
}).mount("#app");
</script>
Check what you learned in “Vue Computed Properties”.
Declare computed properties.
: { total() { return this.a + this.b; } }
What is special about computed properties?
Add a computed property fullName that returns first + " " + last and show it in the <p>.
Free sign-in required · Google login available
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services