从Vuex存储中获取数组长度

huangapple go评论62阅读模式
英文:

how to get an array length from vuex store

问题

I understand that you want a translation of the code and related content without translating the code itself. Here's the translation:

请我有这个商店,我试图获取项目的总长度
这是我的商店

当我使用console.log(state.products.length)时,我得到了undefined

这是我的Vue组件

这是我的控制台中的错误

请问我应该如何处理这个问题?

英文:

please i have this store and i'm trying to get the totall length of items
从Vuex存储中获取数组长度

this is my store

const state = {
  products: [],
};

const getters = {
  allProducts: state => state.products,
  totalPercentage(state) {
    const totalProducts = state.products.length;
    console.log(state.products.length)
   return ((totalProducts / 100) * 100).toFixed(2);
  }
};
const mutations = {
  SET_PRODUCTS(state, products) {
    state.products = products;
  }
};

when i console.log(state.products.length) i get undefined

this is my vue component

 <p>Total Percentage: {{ totalPercentage }}</p>
<script>
import { mapGetters } from "vuex";
export default {
  computed: {
    ...mapGetters(["allProducts", "totalPercentage"])
  },
  mounted() {
    this.$store.dispatch("getProducts");
  }
};
</script>

this is error in my console

从Vuex存储中获取数组长度

please how do i go about this

答案1

得分: 1

products 是一个具有 products 属性的对象,该属性是一个数组,因此尝试使用以下代码:

state.products.products.length
英文:

products is object with products property that is array, so try with

state.products.products.length

huangapple
  • 本文由 发表于 2023年5月6日 20:03:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定