Vue實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)功能
本文實(shí)例為大家分享了Vue實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)功能的具體代碼,供大家參考,具體內(nèi)容如下
話(huà)不多少,上效果圖

代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet">
</head>
<body>
<div id="app">
<div v-if="books.length">
<table class="table table-dark">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">書(shū)籍名稱(chēng)</th>
<th scope="col">出版日期</th>
<th scope="col">書(shū)籍價(jià)格</th>
<th scope="col">購(gòu)買(mǎi)數(shù)量</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in books">
<th scope="row">{{item.id}}</th>
<td>{{item.name}}</td>
<td>{{item.date}}</td>
<td>{{item.price | dealPrice}}</td>
<td>
<button class="btn btn-primary" @click="decrement(index)" :disabled="item.count <= 0">-</button>
{{item.count}}
<button class="btn btn-primary" @click="increment(index)">+</button>
</td>
<td>
<button class="btn btn-danger" @click="removeBook(index)">移除</button>
</td>
</tr>
</tbody>
</table>
<h2>總價(jià)為 {{totalPrice | dealPrice}}</h2>
</div>
<h2 v-else>購(gòu)物車(chē)為空</h2>
</div>
<script src="vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
books:[
{
id:1,
name:'PHP手冊(cè)',
date:'2020年5月17號(hào)',
price:33,
count:1
},
{
id:2,
name:'Python手冊(cè)',
date:'2020年5月17號(hào)',
price:33,
count:1
},
{
id:3,
name:'Linux手冊(cè)',
date:'2020年5月17號(hào)',
price:33,
count:1
},
],
},
computed:{
totalPrice(){
let price = 0;
for (let i = 0;i<this.books.length;i++) {
price += this.books[i].price * this.books[i].count
}
return price;
}
},
methods:{
increment(index){
this.books[index].count ++
},
decrement(index) {
this.books[index].count --
},
removeBook(index) {
this.books.splice(index,1)
}
},
filters: {
dealPrice(price){
return '$' + price.toFixed(2)
}
}
})
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue+vant實(shí)現(xiàn)購(gòu)物車(chē)全選和反選功能
- Vue實(shí)現(xiàn)購(gòu)物車(chē)基本功能
- Vuex實(shí)現(xiàn)購(gòu)物車(chē)小功能
- vue實(shí)現(xiàn)購(gòu)物車(chē)列表
- vuex實(shí)現(xiàn)購(gòu)物車(chē)的增加減少移除
- vuex實(shí)現(xiàn)購(gòu)物車(chē)功能
- 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ē)功能
相關(guān)文章
vue父組件給子組件的組件傳值provide inject的方法
在本篇文章里小編給大家整理的是一篇關(guān)于vue父組件給子組件的組件傳值provide inject的方法,需要的朋友們學(xué)習(xí)下。2019-10-10
Vue.js最佳實(shí)踐(五招助你成為vuejs大師)
這篇文章主要介紹了Vue.js最佳實(shí)踐,本文主要面向?qū)ο笫怯幸欢╲ue.js 編輯經(jīng)驗(yàn)的開(kāi)發(fā)者,需要的朋友可以參考下2018-05-05
Vue.js簡(jiǎn)易安裝和快速入門(mén)(第二課)
這篇文章主要為大家詳細(xì)介紹了Vue.js簡(jiǎn)易安裝和快速入門(mén)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
vue?elementui動(dòng)態(tài)添加el-input實(shí)例代碼
最近遇到一個(gè)新的需求,需要?jiǎng)討B(tài)添加el-input,這篇文章主要給大家介紹了關(guān)于vue?elementui動(dòng)態(tài)添加el-input的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
vue使用vuedraggable實(shí)現(xiàn)嵌套多層拖拽排序功能
這篇文章主要為大家詳細(xì)介紹了vue使用vuedraggable實(shí)現(xiàn)嵌套多層拖拽排序功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Vue CLI3基礎(chǔ)學(xué)習(xí)之pages構(gòu)建多頁(yè)應(yīng)用
這篇文章主要給大家介紹了關(guān)于Vue CLI3基礎(chǔ)學(xué)習(xí)之pages構(gòu)建多頁(yè)應(yīng)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Vue CLI3具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
VUE + OPENLAYERS實(shí)現(xiàn)實(shí)時(shí)定位功能
本系列文章介紹一個(gè)簡(jiǎn)單的實(shí)時(shí)定位示例,基于VUE + OPENLAYERS實(shí)現(xiàn)實(shí)時(shí)定位功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-09-09
vue使用elementUI分頁(yè)如何實(shí)現(xiàn)切換頁(yè)面時(shí)返回頁(yè)面頂部
這篇文章主要介紹了vue使用elementUI分頁(yè)如何實(shí)現(xiàn)切換頁(yè)面時(shí)返回頁(yè)面頂部,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

