使用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="">
商品名稱:<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">
商品名稱: {{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>商品名稱</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ì)新增的商品名稱和單價(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)文章
vue3中reactive數(shù)據(jù)被重新賦值后無(wú)法雙向綁定的解決
這篇文章主要介紹了vue3中reactive數(shù)據(jù)被重新賦值后無(wú)法雙向綁定的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
詳解vue中的動(dòng)態(tài)組件component和keep-alive
這篇文章主要介紹了詳解vue中的動(dòng)態(tài)組件component和keep-alive的相關(guān)資料,這大家需要注意include屬性和exclude屬性只能用一個(gè),不能同時(shí)使用,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-11-11
vue-resouce設(shè)置請(qǐng)求頭的三種方法
本篇文章主要介紹了vue-resouce設(shè)置請(qǐng)求頭的三種方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
vue項(xiàng)目打包優(yōu)化方式(讓打包的js文件變小)
這篇文章主要介紹了vue項(xiàng)目打包優(yōu)化方式(讓打包的js文件變小),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vuex模塊獲取數(shù)據(jù)及方法的簡(jiǎn)單示例
Vuex是一個(gè)專為Vue.js應(yīng)用程序開(kāi)發(fā)的狀態(tài)管理模式,它采用集中式存儲(chǔ)管理應(yīng)用的所有組件的狀態(tài),并以相應(yīng)的規(guī)則保證狀態(tài)以一種可預(yù)測(cè)的方式發(fā)生變化,下面這篇文章主要給大家介紹了關(guān)于vuex模塊獲取數(shù)據(jù)及方法的相關(guān)資料,需要的朋友可以參考下2023-03-03
Vue 中 template 有且只能一個(gè) root的原因解析(源碼分析)
這篇文章主要介紹了Vue 中 template 有且只能一個(gè) root的原因解析,本文從源碼角度分析給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
vue.js中window.onresize的超詳細(xì)使用方法
這篇文章主要給大家介紹了關(guān)于vue.js中window.onresize的超詳細(xì)使用方法,window.onresize 是直接給window的onresize屬性綁定事件,只能有一個(gè),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12

