<div id="example">
	<div class="title">
		<div>商品名称</div>
		<div>单价</div>
		<div>数量</div>
		<div>金额</div>
	</div>
	<div class="content" v-for="(item,index) in shop">
		<div>{{item.name}}</div>
		<div>{{item.price}}</div>
		<div>
			<button v-bind:class="{disable:item.count<=0}" v-on:click="item.count>0?item.count--:null">-</button>
			{{item.count}}
			<button v-on:click="item.count++">+</button>
		</div>
		<div>{{item.price*item.count}}</div>
	</div>
	<p>合计:{{totalprice()}}</p>
</div>
<script type="text/javascript">
var exam = new Vue({
	el:'#example',
			data:{
<pre><code>			shop : [{//定义商品信息数组
				name : '女士连衣裙',
				price : 499,
				count : 0,
				isCheckde:false,
				totals:0
			},{ 
				name : '男士衬衫',
				price : 399,
				count : 0,
				isCheckde:false,
				totals:0
			},
			{
				name : '女士短袖',
				price : 128,
				count : 0,
				isCheckde:false,
				totals:0
			},
			{
				name : '男士短袖',
				price : 138,
				count : 0,
				isCheckde:false,
				totals:0
			}
			]
		},
		methods:{
			totalprice:function(){
				var t=0
				for(var i=0;i&lt;this.shop.length;i++){
					t+=this.shop[i].price*this.shop[i].count
				}
				
				return t
			}
		} 				
</code></pre>
<p>})</p>
</script>
Vue 实现购物车功能:数量减至 0 无法再减

原文地址: https://www.cveoy.top/t/topic/nw7D 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录