vue小案例 ,vue 简单实现购物车功能,vue入门小案例

1.主要是用了computed计算属性,sum的变化依赖goods,页面初始化就可以实现sum的计算。

2.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>vue购物车</title>
		<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
	</head>
	<style type="text/css">
		.box {
			width: 800px;
			margin: 0 auto;
		}

		.table-bordered>tbody>tr>th,
		.table-bordered>tbody>tr>td {
			text-align: center;
		}

		.table>tbody+tbody {
			border: 0;
		}

		.table {
			margin-bottom: 0;
		}

		.sum {
			height: 20px;
			width: 800px;
			margin: 0 auto;
		}
	</style>
	<body>
		<div class="box row" id="demo">
			<table class="table table-bordered">
				<tr>
					<th>ID</th>
					<th>商品名称</th>
					<th>日期</th>
					<th>价格</th>
					<th>数量</th>
					<th>小计</th>
					<th>操作</th>
				</tr>
				<tbody>
					<tr v-for="(item,index) in goods">
						<td>{{index+1}}</td>
						<td>{{item.name}}</td>
						<td>{{item.time}}</td>
						<td>¥{{item.price}}.00</td>
						<td>
							<button type="button" @click="addAndSub(index,'sub')">-</button>
							<span>{{item.count}}</span>
							<button type="button" @click="addAndSub(index,'add')">+</button>
						</td>
						<td>¥{{item.price * item.count}}.00</td>
						<td>
							<a href="javascript:;" @click="del(index)">删除</a>
						</td>
					</tr>
				</tbody>
			</table>
			<div class="sum">
				<span>商品总价:</span>
				<span>¥{{total}}.00</span>
			</div>
		</div>

	</body>
</html>
<script src="js/jquery-3.4.1.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="js/bootstrap.min.js"></script>
<script>
	let time = new Date()
	let y = time.getFullYear()
	let m = time.getMonth() + 1
	let d = time.getDate()
	let H = time.getHours()
	let min = time.getMinutes()
	let myDate = y + '-' + m + '-' + d + '-' + H + ':' + min
	let goods = [{
			name: '炸酱面',
			time: myDate,
			price: 4,
			count: 2,
			sub: 4
		},
		{
			name: '裹粉',
			time: myDate,
			price: 2,
			count: 4,
			sub: 3
		},
		{
			name: '方便面',
			time: myDate,
			price: 5,
			count: 3,
			sub: 5
		},
		{
			name: '洗发水',
			time: myDate,
			price: 2,
			count: 1,
			sub: 2
		},
		{
			name: '沐浴露',
			time: myDate,
			price: 3,
			count: 2,
			sub: 6
		}
	]



	let app = new Vue({
		el: '#demo',
		data: {
			goods,
			// total:0
		},
		// created () {
		// 	this.Total()
		// },
		computed: {
			// 成为了组件中的数据  相当于定义data  这个的值是通过计算出来的
			total: function() {
				// 使用计算属性computed,good变化,sum跟着变化
				let total = 0;
				for (var prop of this.goods) {
					// 总价
					total += prop.price * prop.count
				}
				return total
			}
		},
		methods: {
			// 改变数量
			addAndSub(index, str) {
				if (str === "add") {
					this.goods[index].count++;
				} else {
					this.goods[index].count > 0 ?
						this.goods[index].count--
						:
						(this.goods[index].count = 0);
				}
			},
			del(index) {
				this.goods.splice(index, 1)
			}
		}
	})
</script>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值