import { createStore } from 'vuex'
const store = createStore({
state() {
return {
items: ''
}
},
getters: {
cartItemCount(state) {
return function(count) {
let item = state.items.find(item => item.id === id);
if(item) {
return count;
}
}
}
}
})
export default store
cart.vue
<template>
<a href="javascript:;" @click="cart"></a>
<span>购物车{{ cartItemsCount }}</span>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
name: 'Cart',
components: {},
computed: {
...mapGetters('cart', {
cartItemsCount: 'itemsCount'
})
},
methods: {
cart() {
this.$router.push("/cart")
}
}
}
</script>
<style>
</style>
|