Complete the code to import a Vue Composition API function often used with Pinia stores.
import { defineStore } from 'pinia'; import { [1] } from 'vue';
We use ref to create reactive references in Vue 3, which is often imported alongside stores.
Complete the code to define a Pinia store named 'userStore'.
export const useUserStore = defineStore('[1]', { state: () => ({ name: '', age: 0 }) });
The store id should be a unique string. Here, 'user' is used as the store id.
Fix the error in accessing another store inside a Pinia store action.
import { defineStore } from 'pinia'; import { useCartStore } from './cartStore'; export const useUserStore = defineStore('user', { actions: { addToCart(product) { const cart = [1](); cart.add(product); } } });
To access another store inside a store, you call its use function, here useCartStore().
Fill both blanks to correctly update state from another store inside an action.
export const useOrderStore = defineStore('order', { actions: { placeOrder() { const cart = [1](); this.items = cart.[2]; } }, state: () => ({ items: [] }) });
We get the cart store instance with useCartStore() and access its items state to update the order store.
Fill all three blanks to create a store that reacts to another store's state using a getter.
export const useDiscountStore = defineStore('discount', { getters: { totalDiscount(state) { const cart = [1](); return cart.[2] * state.[3]; } }, state: () => ({ discountRate: 0.1 }) });
The discount store gets the cart store instance with useCartStore(), accesses totalPrice from cart, and multiplies by its own discountRate.