英文:
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
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
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论