note笔记
TabBar.vue
```javascript
<!-- 导航栏组件 -->
<!-- 在路由中跳转路由使用router-link标签编译之后就是a标签
1.router-link上边有一个to属性,属性值就是路由path 可以通过tag属性指定标签名
2.添加完router-link之后会自带一个类名router-link-active
3.在App.vue里的style里:
.router-link-active {
color: #f55;
}
-->
<template>
<div class="tabbar">
<ul>
<router-link to="/home" tag="li">
<div class="icon">
<!-- 未选中 -->
<i class="iconfont icon-shouye" v-if="activePath != '/home'"></i>
<!-- 选中 -->
<i class="iconfont icon-shouyefill" v-else></i>
</div>
<span>首页</span>
</router-link>
<router-link to="/category" tag="li">
<div class="icon">
<!-- 未选中 -->
<i class="iconfont icon-fenlei" v-if="activePath != '/category'"></i>
<!-- 选中 -->
<i class="iconfont icon-fenlei1" v-else></i>
</div>
<span>分类</span>
</router-link>
<router-link to="/find" tag="li">
<div class="icon">
<!-- 未选中 -->
<i class="iconfont icon-faxian" v-if="activePath != '/find'"></i>
<!-- 选中 -->
<i class="iconfont icon-faxianjihuo" v-else></i>
</div>
<span>发现</span>
</router-link>
<router-link to="/cart" tag="li">
<div class="icon">
<!-- 未选中 -->
<i class="iconfont icon-gouwuche" v-if="activePath != '/cart'"></i>
<!-- 选中 -->
<i class="iconfont icon-gouwuchefill" v-else></i>
</div>
<span>购物车</span>
</router-link>
<router-link to="/mine" tag="li">
<div class="icon">
<!-- 未选中 -->
<i class="iconfont icon-wode" v-if="activePath != '/mine'"></i>
<!-- 选中 -->
<i class="iconfont icon-wodexiao" v-else></i>
</div>
<span>我的</span>
</router-link>
</ul>
</div>
</template>
<script>
export default {
components: {},
data() {
return {
activePath: "/home",
};
},
methods: {},
watch: {
$route(to) {
this.activePath = to.path;
},
},
mounted() {
this.activePath = this.$route.path;
},
};
</script>
<style lang="less">
.tabbar {
width: 100%;
height: 4.9rem;
background: #efefef;
position: fixed;
left: 0;
bottom: 0;
color: #666;
ul {
width: 100%;
height: 4.9rem;
display: flex;
justify-content: flex-start;
text-align: center;
align-items: center;
li {
flex: 1;
i {
font-size: 25px;
}
}
}
}
</style>
购物车的全选功能
<!-- 购物车组件 -->
<template>
<div class="cart_page">
<div class="cart-header">头部</div>
<div class="cart-list">
<div
class="shop"
v-for="(cartListData, index) in cartListDatas"
:key="cartListData.shop_id"
>
<div class="shop-name">
<div class="icon" @click="shopSelectFn(index, cartListData)">
<!-- 未选中 -->
<i
class="iconfont icon-weixuanzhong"
v-if="!cartListData.is_shop_select"
></i>
<!-- 选中 -->
<i class="iconfont icon-31xuanzhong" style="color: #f55" v-else></i>
</div>
<span>{{ cartListData.shop_name }}--{{ index }}</span>
</div>
<div class="goods">
<ul>
<li
v-for="(cartList, index) in cartListData.children"
:key="cartList.goods_id"
>
<div class="icon" @click="goodsSelectFn(index, cartList)">
<!-- 未选中 -->
<i
class="iconfont icon-weixuanzhong"
v-if="!cartList.is_goods_select"
></i>
<!-- 选中 -->
<i
class="iconfont icon-31xuanzhong"
style="color: #f55"
v-else
></i>
</div>
<div class="goods-thumb">
<img
src="https://x.dscmall.cn/storage/images/201703/thumb_img/0_thumb_G_1490174741051.jpg"
alt=""
/>
</div>
<div class="goods-info">
<div class="goods-title">
{{ cartList.goods_name }}
</div>
<div class="goods-content">
<div class="goods-price">{{ cartList.shop_price }}</div>
<div class="goods-num">
<a
href=""
@click.prevent="
cartList.buy_num > 1 ? cartList.buy_num-- : 1
"
>-</a
>
<input type="text" v-model="cartList.buy_num" />
<a href="" @click.prevent="cartList.buy_num++">+</a>
<i class="iconfont icon-shoucang"></i>
<i class="iconfont icon-shanchu"></i>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="cart-footer">
<div class="icon" @click="selectAllFn">
<!-- 未选中 -->
<i class="iconfont icon-weixuanzhong" v-if="!selectAll"></i>
<!-- 选中 -->
<i class="iconfont icon-31xuanzhong" style="color: #f55" v-else></i>
<span>全选</span>
</div>
<div class="total">合计:{{ totalPrice }}</div>
<div class="buy">去结算({{ totalGoods }})</div>
</div>
</div>
</template>
<script>
export default {
components: {},
data() {
return {
selectAll: true,
cartListDatas: [
{
shop_id: 1,
shop_name: "自营",
is_shop_select: true,
children: [
{
goods_id: 1001,
shop_id: 1,
goods_name: "冰墩墩",
goods_thumb:
" https://x.dscmall.cn/storage/images/201703/thumb_img/0_thumb_G_1490174741051.jpg",
shop_price: "1",
is_goods_select: true,
buy_num: 1,
},
{
goods_id: 1002,
shop_id: 1,
goods_name: "冰墩墩2",
goods_thumb:
" https://x.dscmall.cn/storage/images/201703/thumb_img/0_thumb_G_1490174741051.jpg",
shop_price: "2",
is_goods_select: true,
buy_num: 1,
},
],
},
{
shop_id: 2,
shop_name: "官方旗舰店",
is_shop_select: true,
children: [
{
goods_id: 2001,
shop_id: 2,
goods_name: "冰墩墩201",
goods_thumb:
" https://x.dscmall.cn/storage/images/201703/thumb_img/0_thumb_G_1490174741051.jpg",
shop_price: "1",
is_goods_select: true,
buy_num: 1,
},
{
goods_id: 2002,
shop_id: 2,
goods_name: "冰墩墩202",
goods_thumb:
" https://x.dscmall.cn/storage/images/201703/thumb_img/0_thumb_G_1490174741051.jpg",
shop_price: "2",
is_goods_select: true,
buy_num: 1,
},
],
},
],
};
},
computed: {
totalPrice() {
let total = 0;
this.cartListDatas.map((item) => {
item.children.map((value) => {
if (value.is_goods_select) {
total += value.buy_num * value.shop_price;
}
});
});
return total;
},
totalGoods() {
let total = 0;
this.cartListDatas.map((item) => {
item.children.map((value) => {
if (value.is_goods_select) {
total += value.buy_num;
}
});
});
return total;
},
},
watch: {},
methods: {
goodsSelectFn(index, cart) {
console.log(index, cart);
cart.is_goods_select = !cart.is_goods_select;
let shop_id = cart.shop_id;
console.log(shop_id);
for (let i = 0; i < this.cartListDatas.length; i++) {
if (this.cartListDatas[i].shop_id == shop_id) {
this.cartListDatas[i].is_shop_select = this.cartListDatas[
i
].children.every((item) => item.is_goods_select == true);
}
}
this.selectAll = this.cartListDatas.every((item) => {
return item.is_shop_select == true;
});
},
selectAllFn() {
this.selectAll = !this.selectAll;
this.cartListDatas.map((item) => {
item.is_shop_select = this.selectAll;
item.children.map((value) => {
value.is_goods_select = this.selectAll;
});
});
},
shopSelectFn(index, cartListData) {
console.log(index, cartListData);
cartListData.is_shop_select = !cartListData.is_shop_select;
cartListData.children.map((item) => {
item.is_goods_select = cartListData.is_shop_select;
});
this.selectAll = this.cartListDatas.every((item) => {
return item.is_shop_select == true;
});
},
},
beforeCreate() {},
created() {},
beforeMount() {},
mounted() {},
beforeUpdate() {},
updated() {},
beforeDestroy() {},
destroyed() {},
activated() {},
};
</script>
<style lang="less">
.cart_page {
width: 100%;
height: auto;
background: #efefef;
.icon {
i {
font-size: 2rem;
}
}
.cart-header {
height: 4.4rem;
width: 100%;
background: #efefef;
line-height: 4.4rem;
font-size: 1.4rem;
}
.cart-list {
width: calc(100% - 2rem);
margin: 1rem;
.shop {
width: 100%;
height: auto;
border-radius: 1rem;
background: #fff;
margin: 1rem 0;
padding: 0.5rem 0;
.shop-name {
height: 4.4rem;
width: 100%;
background: #fff;
line-height: 4.4rem;
font-size: 1.4rem;
border-top-left-radius: 1rem;
border-top-right-radius: 1rem;
display: flex;
justify-content: flex-start;
align-items: center;
.icon {
margin-left: 1rem;
}
}
.goods {
ul {
width: 100%;
height: auto;
li {
height: 7rem;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
.icon {
width: 4rem;
text-align: center;
}
.goods-thumb {
width: 7rem;
height: 7rem;
img {
width: 7rem;
height: 7rem;
}
}
.goods-info {
width: calc(100% - 11rem);
.goods-title {
height: 4rem;
width: 100%;
line-height: 2rem;
font-size: 1.4rem;
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}
.goods-content {
display: flex;
justify-content: space-between;
align-items: center;
height: 3rem;
.goods-price {
font-size: 1.8rem;
color: #f55;
}
.goods-num {
display: flex;
justify-content: center;
align-items: center;
a {
display: block;
width: 2rem;
height: 2rem;
text-align: center;
line-height: 1.6rem;
font-size: 2rem;
background: #efefef;
}
input {
width: 4rem;
border: 1px solid #ccc;
box-sizing: border-box;
font-size: 1.6rem;
height: 2rem;
text-align: center;
}
i {
margin: 0 0.5rem;
}
}
}
}
}
}
}
}
}
.cart-footer {
width: 100%;
height: 4.4rem;
position: fixed;
bottom: 4.9rem;
left: 0;
display: flex;
justify-content: space-between;
align-items: center;
background: #fff;
.buy {
width: 10rem;
height: 4.4rem;
background: #f55;
color: #fff;
text-align: center;
line-height: 4.4rem;
font-size: 1.4rem;
}
}
}
</style>
项目连接
购物车的全选功能