Vue.js Vue不使用基于Vuex getter的计算属性更新组件
我的Vuex商店中有一些产品,包括价格和所有产品的总价。 若要更新产品价格,当调用我的组件时,我将使用Vue.set更改值,并且每个产品的价格将更新我的组件。但是,我有一个总的计算值,这个值永远不会更新 我的代码(我正在使用合成API)Vue.js Vue不使用基于Vuex getter的计算属性更新组件,vue.js,vuex,store,Vue.js,Vuex,Store,我的Vuex商店中有一些产品,包括价格和所有产品的总价。 若要更新产品价格,当调用我的组件时,我将使用Vue.set更改值,并且每个产品的价格将更新我的组件。但是,我有一个总的计算值,这个值永远不会更新 我的代码(我正在使用合成API) setup(){ const getProductsPrices=computed(()=>root.$store.getters['cart/getProductsPrices']); //下面的对象是一个键值,如{“产品名称”:1500,…..} const
setup(){
const getProductsPrices=computed(()=>root.$store.getters['cart/getProductsPrices']);
//下面的对象是一个键值,如{“产品名称”:1500,…..}
const getTotalPrice=计算的(()=>对象
.entries(getProductsPrices.value)
.reduce((计数器,v)=>(计数器+v[1]),0);
//使用reduce方法将总价计算为整数
返回{
//…许多属性
getProductsPrices,
getTotalPrices,
};
}
在我的店里:
const存储={
声明:{
产品价格:{},
},
吸气剂:{
getProductsPrices(状态){
返回状态。产品价格;
},
},
突变:{
更新项目数量(状态,{productKey,QUANTITY}){
//这里还有一件事
state.products_prices[productKey]=state.products_prices[productKey]| 0;//初始化
Vue.set(state.products\u prices,productKey,productsQuantity*product.price);//set
},
},
}
在我的模板中:
<template>
<v-simple-table dense>
<template v-slot:default>
<thead>
<tr>
<th>Type</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr
v-for="product in getProducts"
:key="product.id"
>
<td>{{product.name}}</td>
<td>{{centsToCurrency(getProductsPrices[product.key] || 0)}}</td> <!-- this values updates always :) -->
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="2">Total</td>
<td>{{centsToCurrency(getTotalPrice || 0)}}</td> <!-- This value never updates -->
</tr>
</tfoot>
</template>
</v-simple-table>
</template>
类型
价格
{{product.name}
{{centsToCurrency(getProductsPrices[product.key]| | 0)}
全部的
{{centsToCurrency(getTotalPrice | | 0)}
我认为这可能与.entries(getProductsPrices.value)
中的值有关。它不应该是getProductsPrices
吗,还是我遗漏了什么?