兴国资源网 Design By www.nnzcdc.com
本文实例为大家分享了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 }} 库存: {{ 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");
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
兴国资源网 Design By www.nnzcdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
兴国资源网 Design By www.nnzcdc.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
2024年05月19日
2024年05月19日
- 苏文劭《雨停出来走走》[FLAC/分轨][210.76MB]
- 群星《2024好听新歌04》十倍音质WAV分轨
- 陈宁《弹指之间HQ》头版限量[低速原抓WAV+CUE]
- 陈宁《故人还》HQCDII限量签名版[低速原抓WAV+CUE]
- 苏文劭《春曲(Lessons)》[320K/MP3][39.8MB]
- 苏文劭《春曲(Lessons)》[FLAC/分轨][97.78MB]
- 群星《2006香港高级视听展原音精选 SACD》[ISO][2G]
- 张琍敏1977-枫林小雨[台湾复刻版][WAV+CUE]
- 林一峰2014-COOKINGMUSIC[香港首版][WAV+CUE]
- 张国荣.2003-一切随风【环球】【WAV+CUE】
- 群星1990-6Pair半+城市民歌白金珍藏版[香港首版][WAV+CUE]
- 群星1990-悲情城市SACD[欧洲版][WAV+CUE]
- 吕继宏2010-北国之恋[中唱][WAV+CUE]
- 《许巍4CD合集》[FLAC][1.3G]
- 群星《全民诵唱》宝丽金50周年之本地篇3CD[WAV+CUE][1.8G]