从VUEX中,获取已经加入购物车中的数据;
1需求是获取已经加入购物车中数据的总个数;cartLength,可以在组件中通过this.$store.state获取相关数据,但是这种方法不规范。应该使用vuex中的getter方法。
在cart组件中如何使用vuex中的getter中,除了this.$store.getters.cartLength,有更好方式,引入mapGetters方法,直接从vuex引入使用,具体步骤如下:
改进前:
<template v-slot:center><div>购物车({
{ cartLength }})</div></template>
computed: {
cartLength() {
return this.$store.state.cartList.length;
// return this.$store.getters.cartLength;
},
},
改进后