這篇文章給大家分享的是有關怎么使用Vue實現商品分類菜單數量提示功能的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
為定陶等地區(qū)用戶提供了全套網頁設計制作服務,及定陶網站建設行業(yè)解決方案。主營業(yè)務為成都做網站、網站建設、外貿營銷網站建設、定陶網站設計,以傳統(tǒng)方式定制建設網站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
Goods組件,數量提示功能最終需要在Goods組件內顯示。
<template>
<div class="goods">
<div class="menu-wrapper" ref="menuScroll">
<ul>
<!--專場-->
<li class="menu-item" :class="{'current':currentIndex===0}" @click="selectMenu(0)">
<p class="text">
<img :src="container.tag_icon" v-if="container.tag_icon" class="icon">
{{container.tag_name}}
</p>
</li>
<li
class="menu-item"
v-for="(item,index) in goods"
:class="{'current':currentIndex===index+1}"
@click="selectMenu(index+1)"
>
<p class="text">
<img :src="item.icon" v-if="item.icon" class="icon">
{{item.name}}
</p>
<i class="num" v-show="calculateCount(item.spus)">{{calculateCount(item.spus)}}</i>//通過i標簽展示數量
</li>
</ul>
</div>
<!-- 右側商品列表 -->
<div class="foods-wrapper" ref="foodScroll">
<!--專場-->
<ul>
<li class="container-list food-list-hook">
<div v-for="item in container.operation_source_list">
<img :src="item.pic_url">
</div>
</li>
<!-- 具體分類-->
<li v-for="item in goods" class="food-list food-list-hook">
<h4 class="title">{{item.name}}</h4>
<!--具體商品列表-->
<ul>
<li v-for="food in item.spus" class="food-item">
<div class="icon" :></div>
<div class="content">
<h4 class="name">{{food.name}}</h4>
<p class="desc" v-if="food.description">{{food.description}}</p>
<div class="extra">
<span class="saled">{{food.month_saled_content}}</span>
<span class="praise">{{food.praise_content}}</span>
</div>
<img
class="product"
:src="food.product_label_picture"
v-show="food.product_label_picture"
>
<p class="price">
<span class="text">¥{{food.min_price}}</span>
<span class="unit">/{{food.unit}}</span>
</p>
</div>
<div class="cartcontrol-wrapper">
<Cartcontrol :food="food"></Cartcontrol>
</div>
</li>
</ul>
</li>
</ul>
</div>
<Shopcart :poiInfo="poiInfo" :selectFoods="selectFoods"></Shopcart>
</div>
</template><script>
import BScroll from "better-scroll";
import Shopcart from "components/Shopcart/Shopcart";
import Cartcontrol from "components/Cartcontrol/Cartcontrol";
export default {
data() {
return {
container: {},
goods: [],
poiInfo: {},
listHeight: [],
menuScroll: {},
foodScroll: {},
scrollY: 0
};
},
components: {
BScroll,
Shopcart,
Cartcontrol
},
created() {
this.$axios
.get("api/goods")
.then(response => {
let json = response.data;
if (json.code === 0) {
// 重點
this.container = json.data.container_operation_source;
this.goods = json.data.food_spu_tags;
console.log(this.goods)
this.poiInfo = json.data.poi_info;
this.$nextTick(function() {
this.initScroll();
// 左右聯(lián)動
// 右->左
// 計算區(qū)間
this.caculateHeight();
});
}
})
.catch(function(error) {
// handle error
console.log(error);
});
},
computed: {
// 根據右側判斷左側index
currentIndex() {
for (let i = 0; i < this.listHeight.length; i++) {
let start = this.listHeight[i];
let end = this.listHeight[i + 1];
if (this.scrollY >= start && this.scrollY < end) {
return i;
}
}
return 0;
},
selectFoods() {
let foods = [];
this.goods.forEach(good => {
good.spus.forEach(food => {
if (food.count > 0) {
foods.push(food);
}
});
});
return foods;
}
},
methods: {
head_bg(imgName) {
return "background-image: url(" + imgName + ");";
},
initScroll() {
this.menuScroll = new BScroll(this.$refs.menuScroll, {
click: true
});
this.foodScroll = new BScroll(this.$refs.foodScroll, {
probeType: 3,
click: true
});
this.foodScroll.on("scroll", pos => {
this.scrollY = Math.abs(Math.round(pos.y));
});
},
caculateHeight() {
let foodList = this.$refs.foodScroll.getElementsByClassName(
"food-list-hook"
);
let height = 0;
this.listHeight.push(height);
for (let i = 0; i < foodList.length; i++) {
height += foodList[i].clientHeight;
this.listHeight.push(height);
}
// [0, 215, 1343, 2425, 3483, 4330, 5823, 7237, 8022, 8788, 9443]
},
selectMenu(index) {
// console.log(index);
let foodlist = this.$refs.foodScroll.getElementsByClassName(
"food-list-hook"
);
// 根據下標,滾動到相對應的元素
let el = foodlist[index];
// 滾動到對應元素的位置
this.foodScroll.scrollToElement(el, 100);
},
calculateCount(spus) {
console.log(spus)
let count = 0;
spus.forEach(food => {
if (food.count > 0) {
count += food.count;
}
});
return count;
},
}
};
</script>注意methods方法中的calculateCount函數實現計算個數,數量來自于增減組件內Cartcontrol。
calculateCount(spus) {
console.log(spus)
let count = 0;
spus.forEach(food => {
if (food.count > 0) {
count += food.count;
}
});
return count;
},
以上是spus數據
Cartcontrol組件控制商品增減
通過組件Cartcontrol接受了來自父組件的傳值,并且我們在組件內添加商品的增減功能。
<template>
<div class="cartcontrol">
<transition name="move">
<div class="cart-decrease" @click="decreaseCart" v-show="food.count">
<span class="inner icon-remove_circle_outline"></span>
</div>
</transition>
<div class="cart-count" v-show="food.count">{{food.count}}</div>
<div class="cart-add icon-add_circle" @click="increaseCart">
<i class="bg"></i>
</div>
</div>
</template><script>
import Vue from 'vue'
export default {
props:{
food:{
type:Object
}
},
methods:{
decreaseCart(){
this.food.count--;//this指向了vue實例
},
increaseCart(){
if(!this.food.count){
Vue.set(this.food,'count',1);
}else{
this.food.count++;
}
}
}
};
</script>完善購物車內的數量統(tǒng)計
<template>
<div class="shopcart" :class="{'highligh':totalCount>0}">
<div class="shopcart-wrapper">
<div class="content-left">
<div class="logo-wrapper" :class="{'highligh':totalCount>0}">
<span class="icon-shopping_cart logo" :class="{'highligh':totalCount>0}"></span>
<i class="num" v-show="totalCount">{{totalCount}}</i>
</div>
<div class="desc-wrapper">
<p class="total-price" v-show="totalPrice">¥{{totalPrice}}</p>
<p class="tip" :class="{'highligh':totalCount>0}">另需{{shipping_fee_tip}}</p>
</div>
</div>
<div class="content-right" :class="{'highligh':totalCount>0}">{{payStr}}</div>
</div>
</div>
</template>
<script>
// 導入BScroll
import BScroll from "better-scroll";
export default {
props: {
min_price_tip: {
type: String,
default: ""
},
shipping_fee_tip: {
type: String,
default: ""
},
selectFoods: {
type: Array,
default() {
return [
/* {
min_price: 10,
count: 3
},
{
min_price: 7,
count: 1
} */
];
}
}
},
computed: {
// 總個數
totalCount() {
let num = 0;
this.selectFoods.forEach(food => {
num += food.count;
});
return num;
},
// 總金額
totalPrice() {
let total = 0;
this.selectFoods.forEach(food => {
total += food.min_price * food.count;
});
return total;
},
// 結算按鈕顯示
payStr() {
if (this.totalCount > 0) {
return "去結算";
} else {
return this.min_price_tip;
}
}
},
components: {
BScroll
}
};
</script>現在商品分類菜單的數量提示就完成了。
感謝各位的閱讀!關于“怎么使用Vue實現商品分類菜單數量提示功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
當前名稱:怎么使用Vue實現商品分類菜單數量提示功能
URL分享:http://www.chinadenli.net/article24/pgccje.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供營銷型網站建設、商城網站、網站營銷、自適應網站、網站改版、關鍵詞優(yōu)化
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)