vuex實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車(chē)功能示例
本文實(shí)例講述了vuex實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車(chē)功能。分享給大家供大家參考,具體如下:
購(gòu)物車(chē)組件
<template> <div> <h1>vuex-shopCart</h1> <div class="shop-listbox"> <shop-list/> </div> <h2>已選商品</h2> <div class="shop-cartbox"> <shop-cart/> </div> </div> </template> <script> import shopList from "./shop-list"; import shopCart from './shop-cart'; export default{ name:'shop', components:{ 'shop-list':shopList, 'shop-cart':shopCart } } </script>
商品列表
<template> <div class="shop-list"> <table> <tr class="shop-listtitle"> <td>id</td> <td>名稱(chēng)</td> <td>價(jià)格</td> <td>操作</td> </tr> <tr v-for="item in shopList" class="shop-listinfo"> <td>{{item.id}}</td> <td>{{item.name}}</td> <td>{{item.price}}</td> <td><button @click="addToCart(item)">加入購(gòu)物車(chē)</button></td> </tr> </table> </div> </template> <script> import{mapActions} from "vuex"; export default{ name:'shopList', data(){ return{ } }, computed:{ shopList(){ return this.$store.getters.getShopList } }, methods:{ ...mapActions(['addToCart']) } } </script> <style lang="less" scoped> @import url('../../static/public.less'); </style>
選中商品列表
<template> <div class="shop-list"> <table> <tr class="shop-listtitle"> <td>id</td> <td>名稱(chēng)</td> <td>價(jià)格</td> <td>數(shù)量</td> <td>操作</td> </tr> <tr v-for="item in cartData" class="shop-listinfo"> <td>{{item.id}}</td> <td>{{item.name}}</td> <td>{{item.price}}</td> <td>{{item.num}}</td> <td><button class="shop-dele dele-btn" @click="deletShop(item)">刪除</button></td> </tr> <tr v-if="cartData.length<=0"> <td colspan="5">暫無(wú)數(shù)據(jù)</td> </tr> <tr> <td colspan="2">總數(shù):{{totalNum}}</td> <td colspan="2">總價(jià)格:{{totalPrice}}</td> <td><button class="dele-cart dele-btn" @click="clearCart">清空購(gòu)物車(chē)</button></td> </tr> </table> </div> </template> <script> import {mapGetters,mapActions} from "vuex"; export default{ name:'shopCart', data(){ return{ } }, computed:{ ...mapGetters({ cartData:'addShopList', totalNum:'totalNum', totalPrice:'totalPrice' }) }, methods:{ ...mapActions({ clearCart:'clearToCart', deletShop:'deletToShop' }) } } </script> <style lang="less" scoped> @import url('../../static/public.less'); .dele-btn{ background-color: red !important; } .dele-btn:hover{ background-color: #bd0000 !important; } </style>
vuex 創(chuàng)建
npm install vuex --save
,創(chuàng)建vuex文件夾,在文件夾中創(chuàng)建store.js,引入vuex;
import Vue from "vue"; import Vuex from 'vuex'; import cart from "./modules/cart"; Vue.use(Vuex); export default new Vuex.Store({ modules:{ cart } })
建立一個(gè)模塊文件夾modules,里面創(chuàng)建創(chuàng)建當(dāng)個(gè)store模塊,然后默認(rèn)輸出,在store.js中引入;
const state = { shop_list: [{ id: 11, name: '魚(yú)香肉絲', price: 12, }, { id: 22, name: '宮保雞丁', price: 14 }, { id: 34, name: '土豆絲', price: 10 }, { id: 47, name: '米飯', price: 2 },{ id: 49, name: '螞蟻上樹(shù)', price: 13 }, { id: 50, name: '臘肉炒蒜薹', price: 15 }], add:[] } const getters ={ //獲取商品列表 getShopList:state => state.shop_list, //獲取購(gòu)物車(chē)列表 addShopList:state => { return state.add.map(({id,num})=>{ let product = state.shop_list.find(n => n.id == id); if(product){ return{ ...product, num } } }) }, //獲取總數(shù)量 totalNum:(state,getters) =>{ let total =0; getters.addShopList.map(n=>{ total += n.num; }) return total; }, //計(jì)算總價(jià)格 totalPrice:(state,getters)=>{ let total=0; getters.addShopList.map(n=>{ total += n.num*n.price }) return total; }, } const actions={ //加入購(gòu)物車(chē) addToCart({commit},product){ commit('addCart',{ id:product.id }) }, //清空購(gòu)物車(chē) clearToCart({commit}){ commit('clearCart') }, //刪除單個(gè)物品 deletToShop({commit},product){ commit('deletShop',product) } } const mutations ={ //加入購(gòu)物車(chē) addCart(state,{id}){ let record = state.add.find(n => n.id == id); if(!record){ state.add.push({ id, num:1 }) }else{ record.num++; } }, //刪除單個(gè)物品 deletShop(state,product){ state.add.forEach((item,i)=>{ if(item.id == product.id){ state.add.splice(i,1) } }) }, //清空購(gòu)物車(chē) clearCart(state){ state.add=[]; } } export default{ state, getters, actions, mutations }
希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。
- 基于Vuejs實(shí)現(xiàn)購(gòu)物車(chē)功能
- Vue實(shí)現(xiàn)購(gòu)物車(chē)功能
- Vuejs實(shí)現(xiàn)購(gòu)物車(chē)功能
- vue實(shí)現(xiàn)商城購(gòu)物車(chē)功能
- Vue實(shí)現(xiàn)購(gòu)物車(chē)詳情頁(yè)面的方法
- vue實(shí)現(xiàn)購(gòu)物車(chē)小案例
- vue 實(shí)現(xiàn)購(gòu)物車(chē)總價(jià)計(jì)算
- vue.js實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)功能
- vue實(shí)現(xiàn)購(gòu)物車(chē)功能(親測(cè)可用)
- 前端Vue學(xué)習(xí)之購(gòu)物車(chē)項(xiàng)目實(shí)戰(zhàn)記錄
相關(guān)文章
在Vue中使用防抖與節(jié)流,及this指向的問(wèn)題
這篇文章主要介紹了在Vue中使用防抖與節(jié)流,及this指向的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01vue+elementUI實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了vue+elementUI實(shí)現(xiàn)圖片上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08Vue3組件庫(kù)框架搭建example環(huán)境的詳細(xì)教程
這篇文章主要介紹了Vue3組件庫(kù)框架搭建example環(huán)境的詳細(xì)教程,本文便搭建?example?開(kāi)發(fā)環(huán)境和打包構(gòu)建,并在example中使用組件庫(kù),需要的朋友可以參考下2022-11-11vue + el-form 實(shí)現(xiàn)的多層循環(huán)表單驗(yàn)證
這篇文章主要介紹了vue + el-form 實(shí)現(xiàn)的多層循環(huán)表單驗(yàn)證,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下。2020-11-11uniapp模仿微信實(shí)現(xiàn)聊天界面的示例代碼
這篇文章主要介紹了如何利用uniapp模仿微信,實(shí)現(xiàn)一個(gè)聊天界面。文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Vue有一定的幫助,感興趣的可以了解一下2022-01-01利用Vue v-model實(shí)現(xiàn)一個(gè)自定義的表單組件
本篇文章主要介紹了利用Vue v-model實(shí)現(xiàn)一個(gè)自定義的表單組件的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04vue使用axios獲取不到響應(yīng)頭Content-Disposition的問(wèn)題及解決
這篇文章主要介紹了vue使用axios獲取不到響應(yīng)頭Content-Disposition的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06關(guān)于uniapp的高級(jí)表單組件mosowe-form
這篇文章主要介紹了關(guān)于uniapp的高級(jí)表單組件mosowe-form,由于一些表單標(biāo)簽改來(lái)改去比較繁瑣,重復(fù)性很多,且樣式布局啥的幾乎萬(wàn)變不離其中,為了偷懶,開(kāi)發(fā)了mosowe-form及mosowe-table兩款高級(jí)組件,需要的朋友可以參考下2023-04-04vue時(shí)間格式總結(jié)以及轉(zhuǎn)換方法詳解
項(xiàng)目中后臺(tái)返回的時(shí)間有多種形式,時(shí)間戳、ISO標(biāo)準(zhǔn)時(shí)間格式等,我們需要轉(zhuǎn)化展示成能看的懂得時(shí)間格式,下面這篇文章主要給大家介紹了關(guān)于vue時(shí)間格式總結(jié)以及轉(zhuǎn)換方法的相關(guān)資料,需要的朋友可以參考下2022-12-12