【问题标题】:I'm unable to display vuex state values in a component template我无法在组件模板中显示 vuex 状态值
【发布时间】:2021-07-21 15:12:12
【问题描述】:

我正在使用可组合函数从 Firestore 中获取文档。我已导入返回到 Vuex 商店的对象。当我在我的组件中访问该 firestore 对象时,我意识到它包含在 ComputedRefImpI{ } 中,并且我无法在模板标签中显示值。

这是我的 store/index.js

import { createStore } from 'vuex'
import getCollection from '../composables/getCollection'

function UpdateLocalStorage(cart){
localStorage.setItem('cart', JSON.stringify(cart))
}

export const store = createStore({
state: {
  cart: [],
  all: [],
  error: null
},
getters: {
  cartProducts (state) {
    return  state.cart
  }
}, 
mutations: {
  
  productQuantityInCart (state, product) {
    const item = state.cart.find(i => i.id === product.id)
  
    if(item)  return item.quantity
    else return null
  },
  addToCart (state, product) {
    let item = state.all.find(i => i.id === product.id)
    
    if(item) {
      item.quantity++
    } else {
      state.cart.push({...product, quantity:1})
    }

    UpdateLocalStorage(state.cart)
  },
  setAllOffersFromDB (state) {
    const { documents, error } = getCollection('allOffers')

    if(!documents) {
      state.error = error
    } else {
      state.all = documents
      
    }
  },
  removeFromCart (state, payload) {
    let product = payload
    let item = state.cart.find(i => i.id === product.id)
  
    if(item.quantity > 1) {
      return null
    } else {
      state.cart.filter(i => i.id !== product.id)
      
    }
    UpdateLocalStorage(state.cart)
  },
  cartTotal (state) {
    let total = 0
    state.cart.forEach(i => {
      total += i.quantity * i.price
    })
    return total
  },
  updateCartFromLocalStorage (state) {
    const cart = localStorage.getItem('cart')

    if(cart) {
      state.cart = JSON.parse(cart)
    }
  }
  
},
actions: {
  productQuantity (context, product) {
    context.commit('productQuantityInCart', product)
  }
}

})

这里是 Cart.vue

<template>
      {{ cartProdcts }} // displays all values as shown in the picture attached

      <div v-for="product in cartProducts" :key="product.id">
        {{ product.title }}     // this value is blank inside browser
        {{ product.subtitle }}  // this value is blank inside browser
        {{ product.price }}     // this value is blank inside browser
     </div>
  </template>

  <script>
  import { computed, ref } from 'vue'
  import {useStore}  from 'vuex'
  export default {
   setup() {
    const store = useStore()
    const cartProducts = computed(() => store.getters.cartProducts)
    
    return { cartProducts}
  }
}

当我在 Cart.vue 中对 getter.cartProducts 进行控制台记录时,正确的值会如附图所示显示,但我无法在模板标签中显示值

【问题讨论】:

  • 你能创建一个堆栈闪电战吗?为什么你在 setup() 中使用计算,“const cartProducts = store.getters.cartProducts”然后返回模板对象?

标签: google-cloud-firestore vuex vuejs3 flux vue-composition-api


【解决方案1】:

setup 方法已经返回模板变量,可以在模板中使用,不需要在计算中添加,vuex 是数据流的一种方式。您可以从 store 中获取值暴露给模板。不使用计算此修复问题

<script>


import { computed, ref } from 'vue'
  import {useStore}  from 'vuex'
  export default {
   setup() {
    const store = useStore()
    const cartProducts = store.getters.cartProducts; //  no need to use compute
    
    return { cartProducts}
  }
}
</script>

【讨论】:

  • @kumar 谢谢你,但它没有显示单个产品的值。与普通的 javascript 对象相比,我的挑战是,firestore 返回的对象难以访问值。 firestore 包装在处理程序和目标对象中
  • 你能创建一个stackblitz吗?
猜你喜欢
  • 1970-01-01
  • 2018-06-17
  • 1970-01-01
  • 1970-01-01
  • 2020-09-12
  • 2017-12-05
  • 2019-02-08
  • 2019-10-23
  • 2022-01-22
相关资源
最近更新 更多