Vue實(shí)現(xiàn)購(gòu)物車計(jì)算總價(jià)功能
用Vue實(shí)現(xiàn)一個(gè)購(gòu)物車計(jì)算總價(jià)的功能,供大家參考,具體內(nèi)容如下
代碼
html
<div id="app"> ? ? ? ? <div class="panel panel-info"> ? ? ? ? ? ? <div class="panel-heading"> ? ? ? ? ? ? ? ? <h3 class="panel-title">購(gòu)物車</h3> ? ? ? ? ? ? </div> ? ? ? ? ? ? <div class="panel-body"> ? ? ? ? ? ? ? ? <div class="checkbox"> ? ? ? ? ? ? ? ? ? ? <label> ? ? ? ? ? ? ? ? ? ? ? ? <input type="checkbox" v-model="checkAll"> ? ? ? ? ? ? ? ? ? ? ? ? 全選 ? ? ? ? ? ? ? ? ? ? </label> ? ? ? ? ? ? ? ? </div> ? ? ? ? ? ? ? ? <ul class="list-group"> ? ? ? ? ? ? ? ? ? ? <li class="list-group-item" v-for="(item) in list" :key="item.id"> ? ? ? ? ? ? ? ? ? ? ? ? <div class="checkbox"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? <label> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <input type="checkbox" v-model="item.checked"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {{item.name}}--{{item.price}}*{{item.quantity}} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <button type="button" @click="item.quantity>1?item.quantity-=1:1" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? class="btn btn-success">-</button> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <button type="button" @click="item.quantity+=1" class="btn btn-success">+</button> ? ? ? ? ? ? ? ? ? ? ? ? ? ? </label> ? ? ? ? ? ? ? ? ? ? ? ? </div> ? ? ? ? ? ? ? ? ? ? </li> ? ? ? ? ? ? ? ? </ul> ? ? ? ? ? ? ? ? <p>總價(jià):{{sumPrice}}</p> ? ? ? ? ? ? </div> ? ? ? ? </div> </div>
js
<script src="./libs/vue.js"></script> ? ? <script> ? ? ? ? new Vue({ ? ? ? ? ? ? el: "#app", ? ? ? ? ? ? data: { ? ? ? ? ? ? ? ? list: [ ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? id: 1, ? ? ? ? ? ? ? ? ? ? ? ? name: "小米10", ? ? ? ? ? ? ? ? ? ? ? ? price: 3999, ? ? ? ? ? ? ? ? ? ? ? ? checked: false, ? ? ? ? ? ? ? ? ? ? ? ? quantity: 1 ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? id: 2, ? ? ? ? ? ? ? ? ? ? ? ? name: "榮耀30", ? ? ? ? ? ? ? ? ? ? ? ? price: 2999, ? ? ? ? ? ? ? ? ? ? ? ? checked: false, ? ? ? ? ? ? ? ? ? ? ? ? quantity: 1 ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? id: 3, ? ? ? ? ? ? ? ? ? ? ? ? name: "魅族17", ? ? ? ? ? ? ? ? ? ? ? ? price: 3699, ? ? ? ? ? ? ? ? ? ? ? ? checked: false, ? ? ? ? ? ? ? ? ? ? ? ? quantity: 1 ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? id: 4, ? ? ? ? ? ? ? ? ? ? ? ? name: "蘋果11", ? ? ? ? ? ? ? ? ? ? ? ? price: 5499, ? ? ? ? ? ? ? ? ? ? ? ? checked: false, ? ? ? ? ? ? ? ? ? ? ? ? quantity: 1 ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ], ? ? ? ? ? ? }, ? ? ? ? ? ? // computed計(jì)算屬性, ? ? ? ? ? ? // ?他有一個(gè)特點(diǎn),可以依賴當(dāng)前數(shù)據(jù)改變之后進(jìn)行重新計(jì)算 ? ? ? ? ? ? computed: { ? ? ? ? ? ? ? ? checkAll: { ? ? ? ? ? ? ? ? ? ? //設(shè)置值,當(dāng)點(diǎn)擊全選按鈕的時(shí)候觸發(fā) ? ? ? ? ? ? ? ? ? ? set(v) { ? ? ? ? ? ? ? ? ? ? ? ? this.list.forEach((item) => (item.checked = v)) ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? //取值,當(dāng)列表中的值改變之后觸發(fā),需要return ? ? ? ? ? ? ? ? ? ? get() { ? ? ? ? ? ? ? ? ? ? ? ? return ( ? ? ? ? ? ? ? ? ? ? ? ? ? ? this.list.length === ? ? ? ? ? ? ? ? ? ? ? ? ? ? this.list.filter((item) => item.checked).length ? ? ? ? ? ? ? ? ? ? ? ? ) ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? //計(jì)算總價(jià),選擇被選中的元素 ? ? ? ? ? ? ? ? sumPrice() { ? ? ? ? ? ? ? ? ? ? return this.list.filter((item) => item.checked).reduce((pre, cur) => { ? ? ? ? ? ? ? ? ? ? ? ? return pre + cur.price * cur.quantity ? ? ? ? ? ? ? ? ? ? }, 0) ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? }, ? ? ? ? ? ? methods: { ? ? ? ? ? ? ? ? save() { ? ? ? ? ? ? ? ? ? ? console.log(this.list.filter((item) => item.checked)) ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }) </script>
結(jié)構(gòu)是用bootstrap寫的,記得下載并引入文件
<link rel="stylesheet" href="./bootstrap.min.css" >
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vuejs手把手教你寫一個(gè)完整的購(gòu)物車實(shí)例代碼
- 基于Vuejs實(shí)現(xiàn)購(gòu)物車功能
- vue實(shí)現(xiàn)購(gòu)物車小案例
- Vue實(shí)現(xiàn)購(gòu)物車功能
- vue實(shí)現(xiàn)商城購(gòu)物車功能
- vue 實(shí)現(xiàn)購(gòu)物車總價(jià)計(jì)算
- vue+vant-UI框架實(shí)現(xiàn)購(gòu)物車的復(fù)選框全選和反選功能
- vuex實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車功能示例
- vue實(shí)現(xiàn)購(gòu)物車拋物線小球動(dòng)畫效果的方法詳解
- Vue實(shí)現(xiàn)購(gòu)物車詳情頁(yè)面的方法
相關(guān)文章
vue3動(dòng)態(tài)加載對(duì)話框的方法實(shí)例
對(duì)話框是很常用的組件,在很多地方都會(huì)用到,下面這篇文章主要給大家介紹了關(guān)于vue3動(dòng)態(tài)加載對(duì)話框的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03ElementUI年份范圍選擇器功能實(shí)現(xiàn)
elementUI中有日期范圍組件,月份范圍選擇的,就是沒有年份范圍選擇的,需要加一個(gè)類似風(fēng)格的,下面這篇文章主要給大家介紹了關(guān)于ElementUI年份范圍選擇器功能實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2023-02-02vuex中store存儲(chǔ)store.commit和store.dispatch的區(qū)別及說(shuō)明
這篇文章主要介紹了vuex中store存儲(chǔ)store.commit和store.dispatch的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09VUE3使用Element-Plus時(shí)如何修改ElMessage中的文字大小
在使用Element-plus的Elmessage時(shí)使用默認(rèn)的size無(wú)法滿足我們的需求時(shí),我們可以自定義字體的大小,但是直接重寫樣式后,并沒有起作用,甚至使用::v-deep深度選擇器也沒有效果,本文介紹VUE3使用Element-Plus時(shí)如何修改ElMessage中的文字大小,感興趣的朋友一起看看吧2023-09-09vue轉(zhuǎn)electron項(xiàng)目及解決使用fs報(bào)錯(cuò):Module?not?found:?Error:?Can&apo
這篇文章主要給大家介紹了關(guān)于vue轉(zhuǎn)electron項(xiàng)目及解決使用fs報(bào)錯(cuò):Module?not?found:?Error:?Can‘t?resolve?‘fs‘?in的相關(guān)資料,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11基于Vue的SPA動(dòng)態(tài)修改頁(yè)面title的方法(推薦)
這篇文章主要介紹了基于Vue的SPA動(dòng)態(tài)修改頁(yè)面title的方法,需要的朋友可以參考下2018-01-01