Vue实现购物车

Vue实现购物车

在这里插入图片描述
1、导入Vue要用的js没有的可以点击到官网下载
在这里插入图片描述
最好两个都下
2、新建div取名字<div id="app">
3、写入数据,实例化Vue

var messages = [{
				sname: "城南旧事",
				num: 1,
				price: 48,
				data: "2015-11-19",
				imgae:"img/南城旧事.jpg",
				dataSize:false,
			},
			{
				sname: "飞机模型",
				num: 1,
				price: 28,
				data: "2015-11-19",
				imgae:"img/飞机模型.jpg",
				dataSize:false,
			}
		]
		const app = new Vue({
			el: '#app',

4、定义一个表格,用来存储信息

<table>
					<tr>
						<th v-for="item in title">{{item}}</th>
					</tr>
					<tr v-for="(d,id) in message">
						<td>{{id+1}}</td>
						<td>{{d.sname}}</td>
						<td><img v-bind:src="d.imgae" width="100px" :class="{imgae:d.dataSize}" @mouseover="over(id)" @mouseleave="leave(id)" /></td>
						<td>{{d.data}}</td>
						<td>
							<button v-on:click="subtract(id)" v-if="d.num>0">-</button> {{d.num}}
							<button v-on:click="plus(id)">+</button>
						</td>
						<td>{{decimals(id)}}</td>
						<td><button @click="deleterow(id)">移除</button></td>
					</tr>
				</table>

5、将数据写入表格

<tr>
						<th v-for="item in title">{{item}}</th>
					</tr>
					<tr v-for="(d,id) in message">
						<td>{{id+1}}</td>
						<td>{{d.sname}}</td>
						<td><img v-bind:src="d.imgae" width="100px" :class="{imgae:d.dataSize}" @mouseover="over(id)" @mouseleave="leave(id)" /></td>
						<td>{{d.data}}</td>
						<td>
							<button v-on:click="subtract(id)" v-if="d.num>0">-</button> {{d.num}}
							<button v-on:click="plus(id)">+</button>
						</td>
						<td>{{decimals(id)}}</td>
						<td><button @click="deleterow(id)">移除</button></td>
					</tr>
<h1>总价格:{{littel}}</h1>

7、实现

computed: {
				littel: function() {
					let result = 0;
					for(let i = 0; i < this.message.length; i++) {
						result += this.message[i].price * this.message[i].num;
					}
					return result;

				}
			},

			methods: {
				plus: function(id) {

					this.message[id].num++;

				},
				subtract: function(id) {

					this.message[id].num--;

				},
				decimals: function(id) {
					return this.message[id].price.toFixed(2);
				},
				deleterow: function(id) {
					this.message.splice(id, 1);
				},
				over:function(id){
					this.message[id].dataSize = true;
				},
				leave:function(id){
					this.message[id].dataSize = false;
				}

			}

		})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值