vue實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車(chē)小案例
本文實(shí)例為大家分享了vue實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)的具體代碼,供大家參考,具體內(nèi)容如下
最近在寫(xiě)vue的相關(guān)項(xiàng)目,所以找一些小例子練習(xí)一下,把一個(gè)js的購(gòu)物車(chē)改成vue了
css部分是直接引入的插件
效果圖如下
<template> <div class="head"> ? ? <meta charset="UTF-8"> ? ? <title>我的購(gòu)物車(chē)-品優(yōu)購(gòu)</title> ? ? <meta name="description" content="品優(yōu)購(gòu)JD.COM-專(zhuān)業(yè)的綜合網(wǎng)上購(gòu)物商城,銷(xiāo)售家電、數(shù)碼通訊、電腦、家居百貨、服裝服飾、母嬰、圖書(shū)、食品等數(shù)萬(wàn)個(gè)品牌優(yōu)質(zhì)商品.便捷、誠(chéng)信的服務(wù),為您提供愉悅的網(wǎng)上購(gòu)物體驗(yàn)!" /> ? ? <meta name="Keywords" content="網(wǎng)上購(gòu)物,網(wǎng)上商城,手機(jī),筆記本,電腦,MP3,CD,VCD,DV,相機(jī),數(shù)碼,配件,手表,存儲(chǔ)卡,品優(yōu)購(gòu)" /> <!-- ? ?<!– 引入facicon.ico網(wǎng)頁(yè)圖標(biāo) –>--> <!-- ? ?<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>購(gòu)物車(chē)</b> ? ? ? ? </div> ? ? ? </div> ? ? </div> ? ? <div class="c-container"> ? ? ? <div class="w"> ? ? ? ? <div class="cart-filter-bar"> ? ? ? ? ? <em>全部商品</em> ? ? ? ? </div> ? ? ? ? <!-- 購(gòu)物車(chē)主要核心區(qū)域 --> ? ? ? ? <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">單價(jià)</div> ? ? ? ? ? ? <div class="t-num">數(shù)量</div> ? ? ? ? ? ? <div class="t-sum">小計(jì)</div> ? ? ? ? ? ? <div class="t-action">操作</div> ? ? ? ? ? </div> ? ? ? ? ? <!-- 商品詳細(xì)模塊 --> ? ? ? ? ? <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> ? ? ? ? ? <!-- 結(jié)算模塊 --> ? ? ? ? ? <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">清理購(gòu)物車(chē)</a> ? ? ? ? ? ? </div> ? ? ? ? ? ? <div class="toolbar-right"> ? ? ? ? ? ? ? <div class="amount-sum">已經(jīng)選<em>{{NumSum}}</em>件商品</div> ? ? ? ? ? ? ? <div class="price-sum">總價(jià): <em>{{totalPrice}}</em></div> ? ? ? ? ? ? ? <div class="btn-area">去結(jié)算</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元】經(jīng)典兒童文學(xué)彩圖青少版八十天環(huán)游地球中學(xué)生語(yǔ)文教學(xué)大綱', ? ? ? ? ? PriceItem: 12.6, ? ? ? ? ? PriceSum: 12.6, ? ? ? ? ? ItemNum: 1, ? ? ? ? ? isSelected:false ? ? ? ? }, ? ? ? ? { ? ? ? ? ? imgUrl:'src/assets/upload/p2.jpg', ? ? ? ? ? message:'【2000張貼紙】貼紙書(shū) 3-6歲 貼畫(huà)兒童 貼畫(huà)書(shū)全套12冊(cè) 貼畫(huà) 貼紙兒童 汽', ? ? ? ? ? PriceItem: 24.8, ? ? ? ? ? PriceSum: 24.8, ? ? ? ? ? ItemNum: 1, ? ? ? ? ? isSelected:false ? ? ? ? }, ? ? ? ? { ? ? ? ? ? imgUrl:'src/assets/upload/p3.jpg', ? ? ? ? ? message:'唐詩(shī)三百首+成語(yǔ)故事全2冊(cè) 一年級(jí)課外書(shū) 精裝注音兒童版 小學(xué)生二三年級(jí)課外閱讀書(shū)籍', ? ? ? ? ? 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("該商品已經(jīng)清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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vuejs手把手教你寫(xiě)一個(gè)完整的購(gòu)物車(chē)實(shí)例代碼
- 基于Vuejs實(shí)現(xiàn)購(gòu)物車(chē)功能
- vue實(shí)現(xiàn)購(gòu)物車(chē)小案例
- Vue實(shí)現(xiàn)購(gòu)物車(chē)功能
- vue實(shí)現(xiàn)商城購(gòu)物車(chē)功能
- vue 實(shí)現(xiàn)購(gòu)物車(chē)總價(jià)計(jì)算
- vue+vant-UI框架實(shí)現(xiàn)購(gòu)物車(chē)的復(fù)選框全選和反選功能
- vuex實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車(chē)功能示例
- Vue實(shí)現(xiàn)購(gòu)物車(chē)詳情頁(yè)面的方法
- vue實(shí)現(xiàn)購(gòu)物車(chē)拋物線(xiàn)小球動(dòng)畫(huà)效果的方法詳解
相關(guān)文章
從源碼角度來(lái)回答keep-alive組件的緩存原理
這篇文章主要介紹了從源碼角度來(lái)回答keep-alive組件的緩存原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01vue實(shí)現(xiàn)在進(jìn)行增刪改操作后刷新頁(yè)面
這篇文章主要介紹了vue實(shí)現(xiàn)在進(jìn)行增刪改操作后刷新頁(yè)面,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08vue之組件內(nèi)監(jiān)控$store中定義變量的變化詳解
今天小編就為大家分享一篇vue之組件內(nèi)監(jiān)控$store中定義變量的變化詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11vue實(shí)現(xiàn)兄弟組件之間跳轉(zhuǎn)指定tab標(biāo)簽頁(yè)
這篇文章主要介紹了vue實(shí)現(xiàn)兄弟組件之間跳轉(zhuǎn)指定tab標(biāo)簽頁(yè),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05