Creating an action

Vuex actions are where we should place any logic or asynchronous operations, such as API requests, that don't belong in mutations. As such, they are perfect for enforcing the business rules of our application, such as whether to add a new cart item or update the quantity of an existing item. In the ClientApp/store/actions.js file, add the following exported function:

export const addProductToCart = ({ state, commit }, product) => {  const index = state.cart.findIndex(    i =>      i.productId === product.productId &&      i.colourId === product.colourId &&      i.storageId === product.storageId  );    if (index >= 0) {    commit("updateProductQuantity", index);  } else {    commit("addProductToCart", product);  }};

Actions always receive a context ...

Get ASP.NET Core 2 and Vue.js now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.