vue實現簡單的購物車小案例
本文實例為大傢分享瞭vue實現簡單購物車的具體代碼,供大傢參考,具體內容如下
最近在寫vue的相關項目,所以找一些小例子練習一下,把一個js的購物車改成vue瞭
css部分是直接引入的插件
效果圖如下
<template> <div class="head"> <meta charset="UTF-8"> <title>我的購物車-品優購</title> <meta name="description" content="品優購JD.COM-專業的綜合網上購物商城,銷售傢電、數碼通訊、電腦、傢居百貨、服裝服飾、母嬰、圖書、食品等數萬個品牌優質商品.便捷、誠信的服務,為您提供愉悅的網上購物體驗!" /> <meta name="Keywords" content="網上購物,網上商城,手機,筆記本,電腦,MP3,CD,VCD,DV,相機,數碼,配件,手表,存儲卡,品優購" /> <!-- <!– 引入facicon.ico網頁圖標 –>--> <!-- <link rel="shortcut icon" href="favicon.ico" rel="external nofollow" type="image/x-icon" />--> <!-- 引入css 初始化的css 文件 --> <link rel="stylesheet" href="src/assets/carStyle/base.css" rel="external nofollow" > <!-- 引入公共樣式的css 文件 --> <link rel="stylesheet" href="src/assets/carStyle/common.css" rel="external nofollow" > <!-- 引入car css --> <link rel="stylesheet" href="src/assets/carStyle/car.css" rel="external nofollow" > </div> <div class="body"> <div class="car-header"> <div class="w"> <div class="car-logo"> <img src="src/assets/images/logo.png" alt=""> <b>購物車</b> </div> </div> </div> <div class="c-container"> <div class="w"> <div class="cart-filter-bar"> <em>全部商品</em> </div> <!-- 購物車主要核心區域 --> <div class="cart-warp"> <!-- 頭部全選模塊 --> <div class="cart-thead"> <div class="t-checkbox"> <input type="checkbox" name="" id="" class="checkall"> 全選 </div> <div class="t-goods">商品</div> <div class="t-price">單價</div> <div class="t-num">數量</div> <div class="t-sum">小計</div> <div class="t-action">操作</div> </div> <!-- 商品詳細模塊 --> <div class="cart-item-list" v-for="(good,index) in cartList"> <div class="cart-item"> <div class="p-checkbox"> <input type="checkbox" v-model="good.isSelected" @click="oneSelected(index)"> </div> <div class="p-goods"> <div class="p-img"> <img v-bind:src=good.imgUrl alt=""> </div> <div class="p-msg">{{good.message}}</div> </div> <div class="p-price">{{good.PriceItem}}</div> <div class="p-num"> <div class="quantity-form"> <el-button class="increment" @click="changeDe(index)">-</el-button> <input type="text" class="itxt" v-model=good.ItemNum placeholder="1"> <el-button class="increment" @click="changeIn(index)">+</el-button> </div> </div> <div class="p-sum">{{good.PriceSum}}</div> <div class="p-action"> <el-button type="danger" @click="DeleteItem(id)">刪除</el-button> </div> </div> </div> <!-- 結算模塊 --> <div class="cart-floatbar"> <div class="select-all"> <input type="checkbox" @click="allCheck" v-model="allSelected">全選 </div> <div class="operation"> <a class="remove-batch" @click="DeleteSelected"> 刪除選中的商品</a> <a class="clear-all" @click="DeleteAll">清理購物車</a> </div> <div class="toolbar-right"> <div class="amount-sum">已經選<em>{{NumSum}}</em>件商品</div> <div class="price-sum">總價: <em>{{totalPrice}}</em></div> <div class="btn-area">去結算</div> </div> </div> </div> </div> </div> </div> </template> <script> import { defineComponent } from 'vue' export default defineComponent({ data(){ return{ allSelected:false, cartList:[ { imgUrl:'src/assets/upload/p1.jpg', message:'【5本26.8元】經典兒童文學彩圖青少版八十天環遊地球中學生語文教學大綱', PriceItem: 12.6, PriceSum: 12.6, ItemNum: 1, isSelected:false }, { imgUrl:'src/assets/upload/p2.jpg', message:'【2000張貼紙】貼紙書 3-6歲 貼畫兒童 貼畫書全套12冊 貼畫 貼紙兒童 汽', PriceItem: 24.8, PriceSum: 24.8, ItemNum: 1, isSelected:false }, { imgUrl:'src/assets/upload/p3.jpg', message:'唐詩三百首+成語故事全2冊 一年級課外書 精裝註音兒童版 小學生二三年級課外閱讀書籍', PriceItem: 29.8, PriceSum: 29.8, ItemNum: 1, isSelected:false }, ] } }, methods:{ changeIn(id){ let n=this.cartList[id].ItemNum; // console.log(n); this.cartList[id].ItemNum++; this.cartList[id].PriceSum = this.cartList[id].ItemNum * this.cartList[id].PriceItem; }, changeDe(id){ if(this.cartList[id].ItemNum === 0)alert("該商品已經清0!"); else { this.cartList[id].ItemNum--; this.cartList[id].PriceSum = this.cartList[id].ItemNum * this.cartList[id].PriceItem; } }, DeleteItem(id){ // console.log(this.$parent); // console.log(this.$children); this.cartList.splice(id,1); }, allCheck(){ // console.log(this.allSelected); this.cartList.forEach((item)=>{ // console.log(this.allSelected); // console.log(this.cartList); item.isSelected=!this.allSelected; }) }, DeleteSelected(){ for(let i=0;i<this.cartList.length;i++){ if(this.cartList[i].isSelected===true){ this.cartList.splice(i,1); } } }, DeleteAll(){ this.cartList.splice(0,this.cartList.length); }, oneSelected(id){ if(this.cartList[id].isSelected===false) this.cartList[id].isSelected=true; else this.cartList[id].isSelected=false; let n=0; for(let i=0;i<this.cartList.length;i++){ if(this.cartList[i].isSelected===true) n++; } if(n===this.cartList.length)this.allSelected=true; else this.allSelected=false; } }, computed:{ NumSum() { let num=0; this.cartList.forEach((item)=>{ if(item.isSelected===true) num++; }) return num; }, totalPrice() { let totalp=0; this.cartList.forEach((item)=>{ if(item.isSelected===true) totalp += item.PriceSum; }) return totalp; }, } }) </script> <style lang="scss" scoped> </style>
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。