這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)使用vue怎么編寫一個(gè)購(gòu)物車功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

具體內(nèi)容如下
// An highlighted block
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.cl{
border: 1px solid black;
width: 300px;
margin: 30px;
padding: 15px;
}
</style>
</head>
<body>
<div id="app">
<div class="cl" v-for="(item,index) in items">
<h5>{{item.sname}}</h5>
<button @click="item.sum==0?item.sum==0:item.sum--">-</button>
<span>{{item.sum}}</span>
<button @click="item.sum++">+</button>
<span>{{item.sprice}}元/斤</span>
</div>
<table>
<tr>
<th>選擇</th>
<th>商品名</th>
<th>商品數(shù)量</th>
<th>商品價(jià)格</th>
</tr>
<tr v-for="(item,index) in items">
<td><input type="checkbox" :checked="item.isSelect" @click="item.isSelect=!item.isSelect"></td>
<td>{{item.sname}}</td>
<td>{{item.sum}}</td>
<td>{{item.sprice*item.sum}}</td>
</tr>
<tr>
<td><input type="checkbox" @click="selectProduct(isSelectAll)" :checked="isSelectAll">全選</tf>
<td>合計(jì)</tf>
<td>{{total.totalNum}}</tf>
<td>{{total.totalPrice}}</tf>
</tr>
</table>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
new Vue({
el:'#app',
data:{
items:[
{
'sname':'蘋果',
'sum':1,
'sprice':'10',
},
{
'sname':'香蕉',
'sum':1,
'sprice':'12',
},
{
'sname':'橘子',
'sum':1,
'sprice':'8',
},
]
},
methods:{
selectProduct:function(_isSelect){
for(var i=0 ,len=this.items.length;i<len;i++){
this.items[i].isSelect=!_isSelect;
}
},
},
computed:{
isSelectAll:function(){
return this.items.every(function(val){return val.isSelect})
},
total:function(){
var prolist = this.items.filter(function(val){return val.isSelect});
totalpri = 0;
totalnum = 0;
for (var i = 0;i<prolist.length;i++){
totalpri+=prolist[i].sprice*prolist[i].sum;
totalnum+=prolist[i].sum
}
return{totalNum:totalnum,totalPrice:totalpri}
}
},
mounted:function(){
var _this=this;
this.items.map(function(item){
_this.$set(item,'isSelect',false)
})
}
})
</script>
</body>
</html>上述就是小編為大家分享的使用vue怎么編寫一個(gè)購(gòu)物車功能了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
標(biāo)題名稱:使用vue怎么編寫一個(gè)購(gòu)物車功能-創(chuàng)新互聯(lián)
當(dāng)前鏈接:http://www.chinadenli.net/article28/dgeijp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)站維護(hù)、App設(shè)計(jì)、自適應(yīng)網(wǎng)站、云服務(wù)器、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容