Providing a store in Vue

Vue interview exercise on Provide, inject & stores, difficulty 2 of 3. Solve it in the editor beside a worked example; the tests run the moment you do.

Export CartKey and CartProvider, which provides { items (read-only), add(name, price), remove(name) } over a reactive list, rendering its default slot. Then AddToCart with props `name` and `price` (<button>Add name</button> calling add) and CartSummary (<p>N items · total</p>, and a <button class="clear"> that removes every item). Consumers must not be able to push into the list themselves: provide it through readonly().

Each exercise pairs a worked example with a twin problem to solve in a real editor; the tests run the moment you do. Progress is saved in your browser, and in your account if you sign in.

More Provide, inject & stores in Vue

All Vue exercises · Other languages and frameworks