2019-09-17 14:51:59 4529瀏覽
今天千鋒扣丁學(xué)堂HTML5培訓(xùn)老師給解答分享一篇關(guān)于vue.js購(gòu)物車添加商品組件的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),下面我們一起來(lái)看一下吧。
<template> <div class="cartcontrol"> <!--商品減一區(qū)域--> <div class="reduce" v-show="food.count>0"> <i class="icon-remove_circle_outline"></i> </div> <!--商品數(shù)量區(qū)域--> <div class="num" v-show="food.count>0">4</div> <!--商品加一區(qū)域--> <div class="add" @click="addCart"> <i class="icon-add_circle"></i> </div> </div> </template>
<script> export default { name: "Cartcontrol", props:{ food:{ type:Object } }, methods:{ //添加購(gòu)物車商品數(shù)量 addCart(ele){ if(!ele._constructed){ //better-scroll的派發(fā)事件scroll的event和pc端瀏覽器的點(diǎn)擊事件的event有個(gè) // 屬性區(qū)別_constructed,pc端瀏覽器的點(diǎn)擊事件的event中是沒有這個(gè)屬性的 return; } //一開始food中是沒有商品數(shù)量count if(!this.food.count){ // this.food.count = 1;count不是food對(duì)象中的屬性,直接這樣寫,在dom渲染的時(shí)候是無(wú)法感應(yīng)到count的變化 this.$set(this.food,'count',1); }else{ this.food.count++; } console.log(this.food.count); } } } </script>
<style scoped lang="stylus"> .cartcontrol display flex height .48rem align-items center .num font-size.2rem width .48rem text-align center color rgb(147,153,159) .reduce,.add font-size .4rem color rgb(0,160,220) </style>
addCart(ele){ if(!ele._constructed){ //better-scroll的派發(fā)事件scroll的event和pc端瀏覽器的點(diǎn)擊事件的event有個(gè) // 屬性區(qū)別_constructed,pc端瀏覽器的點(diǎn)擊事件的event中是沒有這個(gè)屬性的 return; } //一開始food中是沒有商品數(shù)量count if(!this.food.count){ this.food.count = 1;count不是food對(duì)象中的屬性,直接向food添加新屬性count, // 當(dāng)count值發(fā)生變化的時(shí)候在dom渲染的時(shí)候是無(wú)法感應(yīng)到count的變化 }else{ this.food.count++; } console.log(this.food.count); }
if(!ele._constructed){ //better-scroll的派發(fā)事件scroll的event和pc端瀏覽器的點(diǎn)擊事件的event有個(gè) // 屬性區(qū)別_constructed,pc端瀏覽器的點(diǎn)擊事件的event中是沒有這個(gè)屬性的 return; } //一開始food中是沒有商品數(shù)量count if(!this.food.count){ // this.food.count = 1;count不是food對(duì)象中的屬性,直接向food添加新屬性count, // 當(dāng)count值發(fā)生變化的時(shí)候在dom渲染的時(shí)候是無(wú)法感應(yīng)到count的變化 this.$set(this.food,'count',1); }else{ this.food.count++; } console.log(this.food.count); }
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】 【掃碼進(jìn)入HTML5前端開發(fā)VIP免費(fèi)公開課】
查看更多關(guān)于“HTML5開發(fā)技術(shù)資訊”的相關(guān)文章>>