這篇文章將為大家詳細講解有關(guān)Vuejs如何實現(xiàn)購物車功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
在鐵山港等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計 網(wǎng)站設(shè)計制作定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),營銷型網(wǎng)站,外貿(mào)網(wǎng)站建設(shè),鐵山港網(wǎng)站建設(shè)費用合理。
1、js屬于一種解釋性腳本語言;2、在絕大多數(shù)瀏覽器的支持下,js可以在多種平臺下運行,擁有著跨平臺特性;3、js屬于一種弱類型腳本語言,對使用的數(shù)據(jù)類型未做出嚴格的要求,能夠進行類型轉(zhuǎn)換,簡單又容易上手;4、js語言安全性高,只能通過瀏覽器實現(xiàn)信息瀏覽或動態(tài)交互,從而有效地防止數(shù)據(jù)的丟失;5、基于對象的腳本語言,js不僅可以創(chuàng)建對象,也能使用現(xiàn)有的對象。
功能概述
學習了Vue.JS的一些基礎(chǔ)知識,現(xiàn)在利用指令、數(shù)據(jù)綁定這些基礎(chǔ)知識開發(fā)一個簡單的購物車功能。功能要點如下:
(1)展示商品的名稱、單價和數(shù)量;
(2)商品的數(shù)量可以增加和減少;
(3)購物車的總價要實時更新,即數(shù)量發(fā)生變動,總價也要相應(yīng)的改變;
(4)商品可以從購物車中移除;
(5)具有選擇功能,只計算選中的商品的總價。
上一張截圖,如下:
代碼
代碼分成三部分,分別是HTML、JS、CSS。關(guān)鍵的是HTML和JS。
HTML
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 購物車</title> <script src="../js/vue.min.js"></script> <link href="../css/cart.css" rel="external nofollow" rel="stylesheet"> </head> <body> <div id="app" v-cloak> <template v-if="list.length"> <table> <thead> <tr> <th><input type="checkbox" v-on:click="swapCheck" v-model="checked"></th> <th>商品名稱</th> <th>商品單價</th> <th>商品數(shù)量</th> <th>操作</th> </tr> </thead> <tbody> <tr v-for="(item,index) in list"> <td><input type="checkbox" v-model="selectList" :id="item.id" :value="index" name="selectList" ></td> <td>{{ item.name }}</td> <td>{{ item.price }}</td> <td> <button @click="handleReduce(index)" :disabled="item.count === 1">-</button> {{ item.count }} <button @click="handleAdd(index)">+</button> </td> <td><button @click="handleRemove(index)">移除</button></td> </tr> </tbody> </table> <div>總價:¥ {{ totalPrice }}</div> </template> <div v-else>購物車為空!</div> </div> <script src="../js/cart.js"></script> </body> </html>
JS
var app = new Vue({ el:'#app', data:{ list:[ { id:1, name:'iPhone 8', price:8888, count:1 }, { id:2, name:'Huwei Mate10', price:6666, count:1 }, { id:3, name:'Lenovo', price:6588, count:1 } ], selectList:[], checked:false }, computed:{ totalPrice:function(){ var total = 0; for(var i = 0,len = this.selectList.length;i < len;i++){ var index = this.selectList[i]; var item = this.list[index]; if(item){ total += item.price * item.count; } else{ continue; } } return total.toString().replace(/\B(?=(\d{3})+$)/g,','); } }, methods:{ handleReduce:function(index){ var item = this.list[index]; if(item.count < 2){ return; } item.count--; }, handleAdd:function(index){ var item = this.list[index]; item.count++; }, handleRemove:function(index){ this.list.splice(index,1); }, swapCheck:function(){ var selectList = document.getElementsByName('selectList'); var len = selectList.length; if(this.checked){ for(var i = 0;i < len;i++){ var item = selectList[i]; item.checked = false; } this.checked = false; this.selectList = []; } else{ for(i = 0;i < len;i++){ item = selectList[i]; if(item.checked === false){ item.checked = true; this.selectList.push(selectList[i].value); } } this.checked = true; } } } });
CSS
[v-cloak]{ display: none; } table{ border: 1px solid black; border-collapse: collapse; border-spacing: 0; empty-cells: show; } th,td{ padding: 8px 16px; border:1px solid black; text-align: center; } th{ background-color: gray; }
關(guān)于“Vuejs如何實現(xiàn)購物車功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
本文標題:Vuejs如何實現(xiàn)購物車功能
本文路徑:http://aaarwkj.com/article8/jjgsip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站排名、微信小程序、服務(wù)器托管、品牌網(wǎng)站制作、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)