2019-09-17 14:51:59 2945浏览
今天千锋扣丁学堂HTML5培训老师给解答分享一篇关于vue.js购物车添加商品组件的方法,本文通过实例代码给大家介绍的非常详细,下面我们一起来看一下吧。
<template> <div class="cartcontrol"> <!--商品减一区域--> <div class="reduce" v-show="food.count>0"> <i class="icon-remove_circle_outline"></i> </div> <!--商品数量区域--> <div class="num" v-show="food.count>0">4</div> <!--商品加一区域--> <div class="add" @click="addCart"> <i class="icon-add_circle"></i> </div> </div> </template>
<script> export default { name: "Cartcontrol", props:{ food:{ type:Object } }, methods:{ //添加购物车商品数量 addCart(ele){ if(!ele._constructed){ //better-scroll的派发事件scroll的event和pc端浏览器的点击事件的event有个 // 属性区别_constructed,pc端浏览器的点击事件的event中是没有这个属性的 return; } //一开始food中是没有商品数量count if(!this.food.count){ // this.food.count = 1;count不是food对象中的属性,直接这样写,在dom渲染的时候是无法感应到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的派发事件scroll的event和pc端浏览器的点击事件的event有个 // 属性区别_constructed,pc端浏览器的点击事件的event中是没有这个属性的 return; } //一开始food中是没有商品数量count if(!this.food.count){ this.food.count = 1;count不是food对象中的属性,直接向food添加新属性count, // 当count值发生变化的时候在dom渲染的时候是无法感应到count的变化 }else{ this.food.count++; } console.log(this.food.count); }
if(!ele._constructed){ //better-scroll的派发事件scroll的event和pc端浏览器的点击事件的event有个 // 属性区别_constructed,pc端浏览器的点击事件的event中是没有这个属性的 return; } //一开始food中是没有商品数量count if(!this.food.count){ // this.food.count = 1;count不是food对象中的属性,直接向food添加新属性count, // 当count值发生变化的时候在dom渲染的时候是无法感应到count的变化 this.$set(this.food,'count',1); }else{ this.food.count++; } console.log(this.food.count); }
【关注微信公众号获取更多学习资料】 【扫码进入HTML5前端开发VIP免费公开课】