Vuex实现简单购物车

作者:jinmo277 时间:2024-05-08 10:43:45 

本文实例为大家分享了Vuex实现简单购物车的具体代码,供大家参考,具体内容如下

文件结构

Vuex实现简单购物车

App.vue


<template>
<div id="app">
<h3>Shopping Cart Demo</h3>
<hr>
<h4>Products:</h4>
<ProductList />
<hr>
<h4>My Cart</h4>
<ShoppingCart />
</div>
</template>
<script>
import ProductList from '@/components/ProductList';
import ShoppingCart from '@/components/ShoppingCart';

export default {
components: {
ProductList,
ShoppingCart
}
}
</script>

Products.vue


<template>
<div>
 <ul v-for="item in products" :key="item.id">
 <li>
  {{ item.title }} - {{ item.price }} &nbsp;&nbsp; 库存: {{ item.inventory }}<br>
  <button :disabled="!item.inventory" @click="addToCart(item)">add to cart</button>
 </li>
 </ul>
</div>

</template>

<script>
import { mapGetters, mapActions } from 'vuex';
export default {
computed: {
// ...mapGetters('products',{
// products: 'allProducts'
// })
...mapGetters({
 products: 'products/allProducts'
})
},
methods: {
...mapActions('cart',['addToCart'])
},
created() {
this.$store.dispatch('products/getAllProducts');
}

}
</script>

ShoppingCart.vue


<template>
<div>
<ul v-for="item in products" :key="item.id">
 <li>{{ item.title }} *{{ item.quantity }}</li>
</ul>
<div>total: {{ total }}</div>
</div>
</template>
<script>
import { mapGetters } from 'vuex';
export default {
computed: {
...mapGetters('cart', {
 products: 'cartProducts',
 total: 'cartTotalPrice'
})
}
}
</script>

modules/products.js


import api from '../../api';

const state = {
all: []
}

const getters = {
allProducts: state => state.all
}

const actions = {
// 获取初始商品数据
getAllProducts({ commit }) {
api.getProducts(products => commit('setProducts', products));
}
}

const mutations = {
setProducts(state, products) {
state.all = products;
},
// 减少该商品库存
decreamentInventory(state, { id }) {
let productItem = state.all.find(item => item.id === id);
productItem.inventory --;
}
}

export default {
namespaced: true,
state,
getters,
actions,
mutations
}

modules/cart.js


const state = {
addedList: []
}

const getters = {
cartProducts(state, getters, rootState) {
return state.addedList.map((item, index) => {
 let productItem = rootState.products.all.find(product => product.id === item.id);
 return {
 title: productItem.title,
 price: productItem.price,
 quantity: item.quantity
 }
})
},
cartTotalPrice(state, getters) {
return getters.cartProducts.reduce((total, product) => {
 return total + (product.price * product.quantity);
}, 0);
}
}

const actions = {
addToCart({ state, commit }, product) {
if (product.inventory > 0) {
 let productItem = state.addedList.find(item => item.id === product.id);
 if (productItem) {
 commit('increamentItemQuantity', productItem);
 } else {
 commit('pushItemToCart', product);
 }
 commit('products/decreamentInventory', product, { root: true });
}
}
}

const mutations = {
// 增加购物车中同等商品的数量
increamentItemQuantity(state, { id }) {
let productItem = state.addedList.find(item => item.id === id);
productItem.quantity ++;
},
// 将商品加入购物车
pushItemToCart(state, { id }) {
state.addedList.push({
 id,
 quantity: 1
})
},

}

export default {
namespaced: true,
state,
getters,
actions,
mutations
}

store/index.js


import Vue from 'vue';
import Vuex from 'vuex';
import cart from './modules/cart';
import products from './modules/products';

Vue.use(Vuex);

export default new Vuex.Store({
modules: {
cart,
products
}
});

main.js


import Vue from "vue";
import App from "@/components/App.vue";
import store from "@/store";

Vue.config.productionTip = false;

new Vue({
store,
render: h => h(App)
}).$mount("#app");

来源:https://blog.csdn.net/jinmo277/article/details/91569317

标签:vuex,购物车
0
投稿

猜你喜欢

  • IE和Firefox下event事件杂谈

    2024-04-22 13:25:33
  • MySQL 分表分库怎么进行数据切分

    2024-01-14 06:15:29
  • keras 获取某层的输入/输出 tensor 尺寸操作

    2021-11-14 09:55:06
  • 停止事件冒泡和阻止浏览器默认行为的脚本

    2008-09-09 22:09:00
  • Python爬取网易云音乐热门评论

    2022-06-16 00:24:58
  • Golang切片Slice功能操作详情

    2024-04-29 13:06:54
  • MySQL安全性指南 (1)(转)

    2010-07-27 12:49:00
  • C++调用python(执行py文件)的全过程

    2022-04-24 00:30:53
  • golang gorm的预加载及软删硬删的数据操作示例

    2024-04-25 13:18:34
  • python matplotlib库的基本使用

    2023-07-20 11:22:45
  • php fopen()函数案例详解

    2023-07-16 22:21:26
  • 总结python多进程multiprocessing的相关知识

    2022-12-04 00:17:57
  • Mysql 插入中文及中文查询 (修改+调试)

    2024-01-21 23:52:42
  • 关于reduce的介绍及用法说明

    2024-04-18 10:49:34
  • mysql和oracle默认排序的方法 - 不指定order by

    2024-01-27 17:44:28
  • Oracle 子程序参数模式,IN,OUT,NOCOPY

    2009-10-23 18:08:00
  • zabbix 监控mysql的方法

    2024-01-16 02:11:04
  • vue项目中less的一些使用小技巧

    2023-07-02 16:51:33
  • python爬取代理ip的示例

    2022-01-20 11:41:12
  • JS组件Bootstrap Select2使用方法解析

    2024-04-16 09:49:36
  • asp之家 网络编程 m.aspxhome.com