如何解决如何计算购物篮Vuejs Vuex的总价格
我在一家商店中有一个名为basketContents
的数组
赞:
state: {
basketContents: [
{
name: .....
quantity: 2
price:1000
},{
name: .....
quantity: 6
price:120
},.........
]
}
据此,我想计算整个购物篮的总价并将其显示在组件中,有没有简便的方法?每当我更改项目数量时,总价也应更改。 我是新来的。
解决方法
getters: {
total: state => {
return state.basketContents.reduce((acc,val) => acc + val.quantity * val.price,0)
}
}
在您的吸气剂中,您可以添加它。
在您的.vue
文件中:
import { mapGetters } from 'vuex';
// And put in computed:
// mix the getters into computed with object spread operator
...mapGetters([
'total'
])
,
在要显示总价的组件的计算中创建一个名为totalPrice的函数:
computed: {
totalPrice() {
return this.$store.state.basketContents.reduce(
(acc,0
);
},},};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。