Vue购物车-简单案例
文章目录
vue的助学者可通过制作一个简易购物车来练练手
效果:
实现功能:
1.显示商品列表
使用v-for、{{}}绑定
<li>
<div class="shopInfo">
<span class="shop">{{shop}}</span>
<a>领券</a>
</div>
<img :src="url" />
<span class="product">{{product}}</span>
<select name class="typeList">
<option value v-for="opt in type">{{opt}}</option>
</select>
<span class="price">¥{{price}}</span>
<div class="counter">
<button @click="$emit('decrement')">-</button>
<span class="count">{{count}}</span>
<button @click="$emit('increment')">+</button>
</div>
</li>数据示例
data: {
commodities: [
{
shop: "shocrex旗舰店",
imgUrl:
"http://fpoimg.com/80x80?text=Preview&bg_color=0d2a9b&text_color=ffffff",
count: 1,
product: "SHOCREX头戴式高清20倍眼镜",
type: ["四组目镜头", "两组目镜头"],
price: 59.9
}
]
}示例图片使用http://fpoimg.com/生成,是一个非常方便的图片生成网站。
2.简单的计数器
计数器是初学vue时最简单的案例
incrementC(index) {
this.commodities[index].count += 1
},
decrementC(index) {
if (this.commodities[index].count > 0) {
this.commodities[index].count -= 1
}
}3.使用计算属性处理合计金额
底部组件-负责显示结算金额与计算按钮
<template id="foot">
<div class="foot">
<b>
合计:
<span>¥{{total}}</span>
</b>
<button class="totalBtn">结算</button>
</div>
</template>计算属性
computed: {
total() {
let total = 0;
for (let i = 0; i < this.commodities.length; i++) {
total += this.commodities[i].price * this.commodities[i].count;
}
return total;
}
}通过这个简单的案例,可以学习到vue组件、组件之间的传值、计算属性的使用等

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论