Store Actions and Bounds
Part of the Routing and State section of Coddy's Vue journey. Lesson 33 of 55.
Actions describe store operations and keep shared rules in one place. In an options store, regular action methods use this to access state and other store members. A guard inside an action protects every component that calls it.
actions:{
add(){this.count++},
remove(){if(this.count>0)this.count--}
}Use regular methods when relying on the store-bound this; an arrow function captures a different lexical this. Check boundaries before changing state, and test repeated attempts at a boundary as well as one successful call. Components can focus on user interactions while actions enforce the shared rule.
Challenge
MediumImplement store actions add and remove. Add increments boxes by one each time. Remove decrements only when boxes is greater than zero. The two components must share the result and the total getter must remain consistent. Use store methods so the boundary rule lives in one place.
Keep the supplied route readout and bootstrap files.
Try it yourself
<!doctype html><html><head><meta charset="UTF-8"><title>Routing practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Routing and State
4Query State
Reading Query ValuesCommitting Search QueriesPreserving Other FiltersNormalizing Query NumbersRecap: Catalog Filters7Pinia Options Stores
Shared Store StateDerived Store GettersStore Actions and BoundsExtracting Reactive FieldsRecap: Seat Counter2Navigation Actions
Navigation from HandlersReplacing HistoryHistory TravelCurrent Navigation LinksRecap: Save and Review5Nested Layouts
Parent and Child OutletsDefault Child ViewsRelative Child PathsAncestor Navigation StateRecap: Library Workspace8Store Design
Setup Store StructurePatching Related StateResetting Store StateComposing Related StoresRecap: Trip Pack11Workshop Planner Project
Planner RoutesShared Reservations