When the user selects a product by clicking the <
> button, they should be taken to another page that displays the product's details and asks the user how many they would like to add to the cart. This will required carrying the selected product through to a new page, so you will need to use the data store again.Add a new field to the state
section of data-store.js
:
// selected product selectedProduct: null;
Add another function to the mutations
section:
// user selects a product selectProduct(state, product) { state.selectedProduct = product; }
The functions in the mutations
section are comma separated, so make sure to add a comma after previous function.
Now we need to make the <
> button on the products page do something useful. Add the following function to themethods
section of products-list.js
: buyProduct(product) { dataStore.commit("selectProduct", product); window.location = "quantity.html" }
This first line stores the product in the data store, and the second redirects the browser to the "how many do you want to buy" page (adapt the file name to suit).
Import the data store module into the products-list.js
(just like you did in the previous section).
Add a @click.prevent
handler to the <
buyProduct
function. You will need to pass the product
(from the v-for
) as a parameter to this function since we need to identify the product that the user selected. Reload and test. When you click the <
> button for a product you should see that the product has been added to the Vuex object in the session storage.