使用Vue2實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車(chē)功能(可直接使用)
vue2.0實(shí)例簡(jiǎn)單購(gòu)物車(chē)
頁(yè)面展示效果如下:?

該購(gòu)物車(chē)實(shí)現(xiàn)了自動(dòng)計(jì)算小計(jì)、總價(jià)。
代碼實(shí)現(xiàn)
<body>
<div id="app">
<div>
<form action="">
商品名稱(chēng):<input type="text" v-model="productName" name="productName">
商品單價(jià):<input type="text" v-model="productPrice" name="productPrice">
<input type="button" value="添加商品" @click="addProduct">
</form>
</div>
<ul>
<li v-for="(pro,index) in productList" :key="index">
商品名稱(chēng): {{pro.productName}} ---------------- 商品單價(jià): {{pro.productPrice}}
<button @click="addproToCart(index)">添加商品</button>
</li>
</ul>
<cart :cartlist="cartList"></cart>
</div>
<template id="cartHtml">
<div>
<table border="1">
<tr>
<td>商品</td>
<td>商品名稱(chēng)</td>
<td>商品價(jià)格</td>
<td>商品數(shù)量</td>
<td>商品價(jià)格</td>
</tr>
<tr v-for="(pro,index) in cartlist" :key="index">
<td><input type="checkbox" v-model="pro.active"></td>
<td>{{pro.productName}}</td>
<td>{{pro.productPrice}}</td>
<td>
<button @click="reduceProNum(index)">-</button>
{{pro.productNum}}
<button @click="addProNum(index)">+</button>
</td>
<td>{{pro.productPrice * pro.productNum}}</td>
</tr>
<tr>
<td colspan="3">選中的商品:{{activeNum}}/{{cartlist.length}}</td>
<td colspan="2">商品總價(jià):{{totalprice}}</td>
</tr>
</table>
</div>
</template>
</body>js代碼
var cart = {
template:'#cartHtml',
props:['cartlist'],
methods:{
// 商品數(shù)量+1
addProNum(index){
let product = this.cartlist[index];
product.productNum++
},
// 商品數(shù)量-1
reduceProNum(index){
let product = this.cartlist[index];
// 判斷商品數(shù)量是否為1
if(product.productNum==1){
this.cartlist.splice(index,1) // 為1,從數(shù)組中刪除
}else{
product.productNum--
}
}
},
computed:{
activeNum(){
let activeProdctList = this.cartlist.filter(item=>{
return item.active
})
return activeProdctList.length
},
totalprice(){
let result = 0;
for(pro of this.cartlist){
if(pro.active){
result = result + pro.productPrice * pro.productNum
}
}
return result;
}
}
}
var app = new Vue({
el:'#app',
data(){
return{
productName:'',
productPrice:0,
productList:[],
cartList:[]
}
},
methods:{
addProduct(){
// todo 對(duì)新增的商品名稱(chēng)和單價(jià),進(jìn)行驗(yàn)證
// 查找新增的商品是否存在于商品列表中,如果不在存在返回-1
let findindex = this.productList.findIndex(item=>{
return item.productName==this.productName
})
if(findindex==-1){ // 判斷商品列表中是否存在新增商品
// 把新商品添加到商品列表中
this.productList.push({productName:this.productName,productPrice:this.productPrice})
}else{
alert('此商品已經(jīng)存在') // 商品已存在,給出提示
}
},
// 添加商品到購(gòu)物車(chē),index是單擊商品的下標(biāo)
addproToCart(index){
let newproduct = this.productList[index]; // 根據(jù)下標(biāo)從商品列表中取出商品對(duì)象
// 從購(gòu)物車(chē)列表中查找,是否存在新的商品,如果查到返回購(gòu)物車(chē)中的商品
let product = this.cartList.find(item=>{
return item.productName==newproduct.productName
})
if(product){
// 如果有對(duì)應(yīng)商品數(shù)量加1
product.productNum++
}else{
// 沒(méi)有對(duì)應(yīng)商品,添加新的商品到購(gòu)物車(chē)
this.cartList.push({
productName:newproduct.productName,
productPrice:newproduct.productPrice,
productNum:1,
active:true
});
}
console.log(product);
}
},
components:{
cart
}
})到此這篇關(guān)于使用Vue2實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車(chē)功能(可直接使用)的文章就介紹到這了,更多相關(guān)Vue2實(shí)現(xiàn)購(gòu)物車(chē)功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue組件實(shí)現(xiàn)可搜索下拉框擴(kuò)展
這篇文章主要為大家詳細(xì)介紹了vue組件實(shí)現(xiàn)可搜索下拉框的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Vue實(shí)現(xiàn)過(guò)渡效果的基本方法
Vue 提供了一個(gè)強(qiáng)大的過(guò)渡系統(tǒng),可以用于在進(jìn)入、離開(kāi)和列表渲染時(shí)添加各種動(dòng)畫(huà)效果,這些過(guò)渡不僅能夠提升用戶(hù)體驗(yàn),還能使界面更加生動(dòng)和吸引人,本文將介紹 Vue 中實(shí)現(xiàn)過(guò)渡效果的基本方法,并提供使用 setup 語(yǔ)法糖的代碼示例,需要的朋友可以參考下2024-09-09
vue 音樂(lè)App QQ音樂(lè)搜索列表最新接口跨域設(shè)置方法
這篇文章主要介紹了vue 音樂(lè)App QQ音樂(lè)搜索列表最新接口跨域設(shè)置方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
Vue3將虛擬節(jié)點(diǎn)渲染到網(wǎng)頁(yè)初次渲染詳解
這篇文章主要為大家介紹了Vue3將虛擬節(jié)點(diǎn)渲染到網(wǎng)頁(yè)初次渲染詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Vue3封裝ant-design-vue表格的詳細(xì)代碼
這篇文章主要介紹了Vue3封裝ant-design-vue表格,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05

