📖 Scenario: You are building a simple Vue 3 app to manage a shopping cart. You want to keep track of the items in the cart and show the total price dynamically.
🎯 Goal: Create a Vue 3 store with a list of cart items, add a getter to compute the total price of all items, and use that getter in a component.
📋 What You'll Learn
Create a reactive store with a cartItems array containing objects with name and price.
Add a getter called totalPrice that computes the sum of all item prices.
Use the getter in a Vue component to display the total price.
Follow Vue 3 Composition API and store patterns.
💡 Why This Matters
🌍 Real World
Stores with getters are common in Vue apps to keep data organized and compute values like totals, averages, or filtered lists.
💼 Career
Understanding how to create computed getters in stores and use them in components is essential for building maintainable Vue applications.
Progress0 / 4 steps
1
Create the initial cart items array
Create a reactive store object called store with a property cartItems set to an array containing these exact objects: { name: 'Apple', price: 1.2 }, { name: 'Banana', price: 0.8 }, and { name: 'Cherry', price: 2.5 }.
Vue
Hint
Use an object named store with a property cartItems that is an array of objects.
2
Add a getter for total price
Add a getter property called totalPrice to the store object. This getter should return the sum of the price values of all items in cartItems using reduce.
Vue
Hint
Use a getter with get totalPrice() and inside use this.cartItems.reduce to sum prices.
3
Create a Vue component to display total price
Create a Vue 3 functional component called TotalPriceDisplay that imports store and uses the totalPrice getter to display the total price inside a <div> with the text: Total Price: $ followed by the value.
Vue
Hint
Use defineComponent and return a render function that shows Total Price: $ plus store.totalPrice.
4
Export the store and component
Add export statements to export both the store object and the TotalPriceDisplay component from the module.
Vue
Hint
Use export const before store and TotalPriceDisplay.
Practice
(1/5)
1. What is the main purpose of getters in a Vue store?
easy
A. To compute and return derived state based on the store's data
B. To directly modify the store's state values
C. To register new components in the Vue app
D. To handle user input events in components
Solution
Step 1: Understand what getters do
Getters compute values derived from the store's state without changing the state itself.
Step 2: Compare options with this role
Only To compute and return derived state based on the store's data describes computing and returning derived state, which matches getters' purpose.
Final Answer:
To compute and return derived state based on the store's data -> Option A
Quick Check:
Getters compute derived state = B [OK]
Hint: Getters compute data, mutations change data [OK]
Common Mistakes:
Confusing getters with mutations that modify state
Thinking getters handle events or components
Assuming getters register components
2. Which of the following is the correct syntax to define a getter named completedTasks in a Vue store?
A. totalPrice: (state) => state.cart.filter(i => i.quantity > 0).reduce((sum, item) => sum + item.price * item.quantity, 0)
B. totalPrice: (state) => state.cart.reduce((sum, item) => sum + item.price * item.quantity, 0)
C. totalPrice: (state) => state.cart.map(i => i.price * i.quantity).reduce((a, b) => a + b)
D. totalPrice: (state) => state.cart.reduce((sum, item) => sum + item.price, 0)
Solution
Step 1: Understand the requirement
Only items with quantity > 0 should be counted in total price calculation.
Step 2: Analyze each option
totalPrice: (state) => state.cart.reduce((sum, item) => sum + item.price * item.quantity, 0) sums all items regardless of quantity. totalPrice: (state) => state.cart.filter(i => i.quantity > 0).reduce((sum, item) => sum + item.price * item.quantity, 0) filters items with quantity > 0 before summing, which is correct. totalPrice: (state) => state.cart.map(i => i.price * i.quantity).reduce((a, b) => a + b) sums all items including those with zero quantity. totalPrice: (state) => state.cart.reduce((sum, item) => sum + item.price, 0) sums only prices ignoring quantity.
Final Answer:
totalPrice: (state) => state.cart.filter(i => i.quantity > 0).reduce((sum, item) => sum + item.price * item.quantity, 0) -> Option A
Quick Check:
Filter then reduce for quantity > 0 total = A [OK]
Hint: Filter items with quantity > 0 before summing [OK]
Common Mistakes:
Not filtering out items with zero quantity
Summing prices without multiplying by quantity
Using map without filtering first
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.