vue實(shí)現(xiàn)購(gòu)物車功能(親測(cè)可用)
本文實(shí)例為大家分享了vue實(shí)現(xiàn)購(gòu)物車功能的具體代碼,供大家參考,具體內(nèi)容如下
如圖,需要有加入購(gòu)物車的標(biāo)識(shí)
思路如下:點(diǎn)擊購(gòu)物車按鈕時(shí)將商品的id,title,imgUrl(海報(bào)圖),flag(標(biāo)識(shí)符,flag非常重要,為以后復(fù)選框判斷是否選中做參考)變成一個(gè)數(shù)組形式,cart,傳入vuex
<template> ?? ?<div> ? <van-goods-action> ? ? <van-goods-action-icon icon="chat-o" text="客服" @click="onClickIcon" /> ? ? <van-goods-action-icon icon="cart-o" text="購(gòu)物車" @click="onClickIcon" /> ? ? <van-goods-action-button ? ? ? type="warning" ? ? ? text="加入購(gòu)物車" ? ? ? @click="onClickButton" ? ? /> ? ? <van-goods-action-button ? ? ? type="danger" ? ? ? text="立即購(gòu)買" ? ? ? @click="onClickButton" ? ? /> ? </van-goods-action> ?? ?</div> </template> <script> ? import { Toast } from 'vant'; ? import { GoodsAction, GoodsActionIcon, GoodsActionButton } from 'vant'; ? export default { ? ? name: 'tabs', ? ? data(){ ? ? ? return{ ? ? ? } ? ? }, ? ? props:{ ? ? ? id:String, ? ? ? current:String, ? ? ? title:String, ? ? ? imgUrl:String ? ? }, ? ? components:{ ? ? ? [Toast.name]: Toast, ? ? ? [GoodsAction.name]: GoodsAction, ? ? ? [GoodsActionIcon.name]: GoodsActionIcon, ? ? ? [GoodsActionButton.name]: GoodsActionButton ? ? }, ? ? ?methods: { ? ? ? ? onClickIcon() { ? ? ? ? ? Toast('點(diǎn)擊圖標(biāo)'); ? ? ? ? }, ? ? ? ? onClickButton() { ? ? ? ? ? var cart={id:this.id,current:this.current,num:1,title:this.title,imgUrl:this.imgUrl,flag:true} ? ? ? ? ? this.$store.commit('addCart',cart) ? ? ? ? ? Toast('已加入購(gòu)物車'); ? ? ? ? }, ? ? ? }, ? ? } </script> <style> </style>
2.vuex如下
import Vue from 'vue' import Vuex from 'vuex' import mutations from './mutations.js' Vue.use(Vuex) export default new Vuex.Store({ ? state: { ?? ?cart:[], ? ? money:0, ? ? allchecked:true ? }, ? mutations, })
export default{、 //判斷是否已經(jīng)加入過(guò)購(gòu)物車,如果加入過(guò),則該產(chǎn)品數(shù)量加一,若沒(méi)有加入過(guò),將產(chǎn)品加入cart中 ? addCart(state,data){ ? ? for(var i=0;i<state.cart.length;i++){ ? ? ? if(state.cart[i].id==data.id){ ? ? ? ? state.cart[i].num+=data.num ? ? ? ? return ? ? ? } ? ? } ? ? state.cart.push(data) ? }, ? //該函數(shù)為數(shù)字+1 ? addCartNum(state,index){ ? ? state.cart[index].num++ ? }, ? ? //該函數(shù)為數(shù)字-1 ? jianCartNum(state,index){ ? ? if(state.cart[index].num==1){return;} ? ? state.cart[index].num-- ? }, }
3.購(gòu)物車
思路如下:若沒(méi)有產(chǎn)品則顯示無(wú)產(chǎn)品,若有產(chǎn)品在購(gòu)物車?yán)?,則可進(jìn)行增刪加減
<template> ? <div class="bg"> ? ? <div class="bgCart" v-if="isGood"> ? ? ? <div class="cartAd"> ? ? ? ? <h3> ? ? ? ? ? 購(gòu)物車 ? ? ? ? </h3> ? ? ? ? <div v-if="select"> ? ? ? ? ? <div class="admin" @click="onAdmin">管理</div> ? ? ? ? </div> ? ? ? ? <div v-if="!select"> ? ? ? ? ? <div class="admin" @click="onOk">完成</div> ? ? ? ? </div> ? ? ? </div> ? ? ? <van-checkbox-group v-model="result" ref="checkboxGroup"> ? ? ? ? <div class="cart" v-for="(item,index) in cartList" :key="index"> ? ? ? ? ? <van-checkbox :name="item.id" class="checkbox" checked-color="red" v-model="item.flag" @click="chooseChange(item.id, item)"> ? ? ? ? ? </van-checkbox> ? ? ? ? ? <div class="box"> ? ? ? ? ? ? <img class="img" :src="baseUrl+item.imgUrl" /> ? ? ? ? ? ? <div class="wraper"> ? ? ? ? ? ? ? <div class="title">{{item.title}}</div> ? ? ? ? ? ? ? <div class="container"> ? ? ? ? ? ? ? ? <div class="money">¥{{item.current}}</div> ? ? ? ? ? ? ? ? <div> ? ? ? ? ? ? ? ? ? <span class="jian" @click="jian(index)"> ? ? ? ? ? ? ? ? ? ? <span class="jianAdj"> -</span> ? ? ? ? ? ? ? ? ? </span> ? ? ? ? ? ? ? ? ? <span>{{item.num}}</span> ? ? ? ? ? ? ? ? ? <span class="jia" @click="add(index)"> ? ? ? ? ? ? ? ? ? ? <span class="jiaAdj"> +</span> ? ? ? ? ? ? ? ? ? </span> ? ? ? ? ? ? ? ? </div> ? ? ? ? ? ? ? </div> ? ? ? ? ? ? </div> ? ? ? ? ? </div> ? ? ? ? </div> ? ? ? </van-checkbox-group> ? ? ? <div class="order" v-if="select"> ? ? ? ? <van-submit-bar :price="getAll" button-text="提交訂單"> ? ? ? ? ? <van-checkbox v-model="allchecked" checked-color="red" @click="allOrder">全選/取消</van-checkbox> ? ? ? ? </van-submit-bar> ? ? ? </div> ? ? ? <div class="order" v-if="!select"> ? ? ? ? <van-submit-bar :price="getAll" button-text="刪除" @submit="del"> ? ? ? ? ? <van-checkbox v-model="allchecked" checked-color="red" @click="allOrder">全選/取消</van-checkbox> ? ? ? ? </van-submit-bar> ? ? ? </div> ?</div> ?<div v-if="!isGood" class="noGood"> ? <h3> ? ? 購(gòu)物車 ? </h3> ? <img src="../../../static/img/cart.jpg"/> ? ?<span class="add"> ? ? ? 您還沒(méi)有添加任何產(chǎn)品哦 ? ?</span> ? ?<van-button round ?type="danger">去逛逛</van-button> ?</div> ? ? ? <bottom></bottom> ? </div> </template> <script> ? import { ? ? Checkbox, ? ? CheckboxGroup ? } from 'vant'; ? import { ? ? SubmitBar ? } from 'vant'; ? import { ? ? Button ? } from 'vant'; ? export default { ? ? name: 'cart', ? ? data() { ? ? ? return { ? ? ? ? result: [], ? ? ? ? cartList: [], ? ? ? ? select: true, ? ? ? ? money: 0, ? ? ? ? results: [], ? ? ? ? baseUrl: this.common.baseUrl, ? ? ? ? allchecked: this.$store.state.allchecked, ? ? ? ? isGood:true ? ? ? } ? ? }, ? ? components: { ? ? ? [SubmitBar.name]: SubmitBar, ? ? ? [Button.name]: Button, ? ? ? [Checkbox.name]: Checkbox, ? ? ? [CheckboxGroup.name]: CheckboxGroup, ? ? ? bottom: () => import('./components/bottom.vue') ? ? }, ? ? computed: { ? ? ? getAll() { ? ? ? ? var money = 0 ? ? ? ? this.$store.state.cart.forEach(item => { ? ? ? ? ? if (item.flag) { ? ? ? ? ? ? money += (item.num * item.current) * 100 ? ? ? ? ? } ? ? ? ? }) ? ? ? ? return money ? ? ? } ? ? }, ? ? methods: { ? ? ? //選擇單個(gè)復(fù)選框(非常重要) ? ? ?//由于我進(jìn)來(lái)是使復(fù)選框全選,則在第一次點(diǎn)擊的時(shí)候使得flag=false ? ? ? chooseChange(i, item) { ? ? ? ? if (this.result.indexOf(i) > -1) { ? ? ? ? ? var arrs = this.result.filter(function(item) { ? ? ? ? ? ? return item != i; ? ? ? ? ? }); ? ? ? ? ? this.result = arrs; ? ? ? ? ? item.flag = false; ? ? ? ? } else { ? ? ? ? ? this.result.push(i); ? ? ? ? ? item.flag = true; ? ? ? ? } ? ? ? ? //判斷單個(gè)和全部,若單個(gè)全選,則this.$store.state.allchecked為true ? ? ? ? if (this.result.length < this.$store.state.cart.length) { ? ? ? ? ? this.$store.state.allchecked = false; ? ? ? ? ? this.allchecked = this.$store.state.allchecked; ? ? ? ? } else { ? ? ? ? ? this.$store.state.allchecked = true; ? ? ? ? ? this.allchecked = this.$store.state.allchecked; ? ? ? ? } ? ? ? }, ? ? ? //全選狀態(tài) ? ? ? allOrder() { ? ? ? ? //如果選擇狀態(tài)為選中的時(shí)候,設(shè)置this.$store.state.allchecked=false變成未選中 ? ? ? ? if (this.$store.state.allchecked) { ? ? ? ? ? this.$store.state.cart.forEach(item => { ? ? ? ? ? ? item.flag = false; ? ? ? ? ? }) ? ? ? ? ? this.result = []; ? ? ? ? ? this.$store.state.allchecked = false; ? ? ? ? ? this.allchecked = this.$store.state.allchecked; ? ? ? ? } else { ? ? ? ? ? this.$store.state.cart.forEach(item => { ? ? ? ? ? ? item.flag = true; ? ? ? ? ? ? if (this.result.indexOf(item.id) < 0) { ? ? ? ? ? ? ? this.result.push(item.id); ? ? ? ? ? ? } ? ? ? ? ? }) ? ? ? ? ? this.$store.state.allchecked = true; ? ? ? ? ? this.allchecked = this.$store.state.allchecked; ? ? ? ? } ? ? ? }, ? ? ? //數(shù)字+ ? ? ? add(index) { ? ? ? ? this.$store.commit('addCartNum', index) ? ? ? }, ? ? ? //數(shù)字減 ? ? ? jian(index) { ? ? ? ? this.$store.commit('jianCartNum', index) ? ? ? }, ? ? ? //點(diǎn)擊管理 ? ? ? onAdmin() { ? ? ? ? this.select = false ? ? ? }, ? ? ? //點(diǎn)擊完成 ? ? ? onOk() { ? ? ? ? this.select = true ? ? ? ? if(this.result.length==0){ ? ? ? ? ? console.log(1) ? ? ? ? ? this.isGood=false ? ? ? ? }else{ ? ? ? ? ? ?console.log(this.result) ? ? ? ? } ? ? ? }, ? ? ? //刪除 ? ? ? del() { ? ? ? ? if (this.result.length == this.$store.state.cart.length) { ? ? ? ? ? this.$store.state.cart.splice(0, this.result.length) ? ? ? ? ? this.result.splice(0, this.result.length) ? ? ? ? } else { ? ? ? ? ? this.$store.state.cart.forEach(item => { ? ? ? ? ? ? if (item.flag) { ? ? ? ? ? ? ? this.$store.state.cart.splice(item, 1) ? ? ? ? ? ? ? this.result.splice(item.id, 1) ? ? ? ? ? ? } ? ? ? ? ? }) ? ? ? ? } ? ? ? } ? ? }, ? ? created() { ? ? ? this.cartList = this.$store.state.cart ? ? ? if (this.$store.state.allchecked) { ? ? ? ? for (var i = 0; i < this.$store.state.cart.length; i++) { ? ? ? ? ? this.result.push(this.$store.state.cart[i].id) ? ? ? ? } ? ? ? } ? ? ? if(this.result.length==0){ ? ? ? ? this.isGood=false ? ? ? } ? ? } ? } </script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于Vuejs實(shí)現(xiàn)購(gòu)物車功能
- Vue實(shí)現(xiàn)購(gòu)物車功能
- Vuejs實(shí)現(xiàn)購(gòu)物車功能
- vue實(shí)現(xiàn)商城購(gòu)物車功能
- vuex實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車功能示例
- Vue實(shí)現(xiàn)購(gòu)物車詳情頁(yè)面的方法
- vue實(shí)現(xiàn)購(gòu)物車小案例
- vue 實(shí)現(xiàn)購(gòu)物車總價(jià)計(jì)算
- vue.js實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車功能
- 前端Vue學(xué)習(xí)之購(gòu)物車項(xiàng)目實(shí)戰(zhàn)記錄
相關(guān)文章
Vue頁(yè)面監(jiān)聽(tīng)鍵盤按鍵的方法總結(jié)
在Vue頁(yè)面中,可以使用多種方法來(lái)監(jiān)聽(tīng)鍵盤按鍵,這篇文章主要為大家整理了五種常用的方法,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考下2023-10-10vue項(xiàng)目從node8.x升級(jí)到12.x后的問(wèn)題解決
這篇文章主要介紹了vue項(xiàng)目從node8.x升級(jí)到12.x后的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10詳解基于Vue-cli搭建的項(xiàng)目如何和后臺(tái)交互
這篇文章主要介紹了詳解基于Vue-cli搭建的項(xiàng)目如何和后臺(tái)交互,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06Vue中安裝element-ui失敗問(wèn)題原因及解決
Vue中安裝element-ui失敗問(wèn)題原因及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01Vue.js 通過(guò)jQuery ajax獲取數(shù)據(jù)實(shí)現(xiàn)更新后重新渲染頁(yè)面的方法
今天小編小編就為大家分享一篇Vue.js 通過(guò)jQuery ajax獲取數(shù)據(jù)實(shí)現(xiàn)更新后重新渲染頁(yè)面的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Vue拿到二進(jìn)制流圖片如何轉(zhuǎn)為正常圖片并顯示
這篇文章主要介紹了Vue拿到二進(jìn)制流圖片如何轉(zhuǎn)為正常圖片并顯示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06vue keep-alive 動(dòng)態(tài)刪除組件緩存的例子
今天小編就為大家分享一篇vue keep-alive 動(dòng)態(tài)刪除組件緩存的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11vue各種事件監(jiān)聽(tīng)實(shí)例(小結(jié))
這篇文章主要介紹了vue各種事件監(jiān)聽(tīng)實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06vue.js給動(dòng)態(tài)綁定的radio列表做批量編輯的方法
下面小編就為大家分享一篇vue.js給動(dòng)態(tài)綁定的radio列表做批量編輯的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02