Estoy recuperando datos de la Tienda Vuex. En primer lugar, quiero verificar si la matriz está presente en la tienda Vuex, luego quiero verificar si el objeto noProducts en el índice 0 no está presente.
La razón de esto es que tweakwiseSortedProducts se usa para ambos productos y un booleano sin productos para reaccionar en el front-end
tweakwiseHasProducts () { if (this.$store.state.tweakwise?.tweakwiseSortedProducts) { return ( this.$store.state.tweakwise.tweakwiseSortedProducts[0].noProducts === false ); } return false; },Mi front-end actualmente, a menudo, devuelve:
this.$store.state.tweakwise.tweakwiseSortedProducts[0] is undefineden la consola
Esto sucede porque tweakwiseSortedProducts no está unificado sino una lista vacía. Puedes probar:
tweakwiseHasProducts () { if (this.$store.state.tweakwise?.tweakwiseSortedProducts?.length !== 0) { return ( this.$store.state.tweakwise.tweakwiseSortedProducts[0].noProducts === false ); } return false; },o solo:
tweakwiseHasProducts () { return this.$store.state.tweakwise?.tweakwiseSortedProducts[0]?.noProducts === false; },que será falso si alguno de estos elementos no está definido, o verdadero si noProducts es realmente falso
Se recomienda usar getter al llamar a un valor en Vuex.
Por favor refiérase a la siguiente.
getters: { getTweakwiseSortedProducts: (state: any) => { return state.tweakwise?.tweakwiseSortedProducts || []; }, }, tweakwiseHasProducts () { this.$store.getters.getTweakwiseSortedProducts.length ? true : false; }