[Vuejs]-How to get total to display on vue component from vuex store?

0👍

I believe that the best would be to calculate the total either with a computed property, so the component won’t recalculate the total too many times, or a Vuex getter could be created using the same logic.

computed: {
  total () {
    this.$store.state.cartdata.reduce((result, product) => {
      // We need to ensure that the property is of the type Number.
      result += Number(product.price);
      return result;
    }, 0);
  },
},

Please note that the property product.price might be wrong, as the data structure wasn’t provided.

Leave a comment