麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JavaScript > 正文

Vue實現商品分類菜單數量提示功能

2019-11-19 11:07:52
字體:
來源:轉載
供稿:網友

如上所示,這篇我們將商品分類菜單顯示數量的提示完善,是軟件更加易于使用。

好先讓我回顧一下上節課的內容,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">     <h3 class="title">{{item.name}}</h3>     <!--具體商品列表-->     <ul>      <li v-for="food in item.spus" class="food-item">               <div class="icon" :style="head_bg(food.picture)"></div>       <div class="content">        <h3 class="name">{{food.name}}</h3>        <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();      // 左右聯動      // 右->左      // 計算區間      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>

完善購物車內的數量統計

<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>// 導入BScrollimport 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實現商品分類菜單數量提示功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 一级黄色大片在线观看 | av7777777| 一二区成人影院电影网 | 日本在线播放一区二区三区 | 国产日韩在线观看视频 | 欧美日韩国产综合网 | 国产亚洲在 | 欧美一级免费在线观看 | 亚洲射逼 | 久久久婷婷一区二区三区不卡 | 久草最新网址 | 蜜桃91麻豆| 一级在线观看视频 | 欧美精品一区二区三区在线 | 亚洲骚综合 | 666sao| 日韩一级免费 | 亚洲成人中文字幕在线 | 欧美成年性h版影视中文字幕 | 中文字幕一区在线观看视频 | 色人久久 | 免费看性xxx高清视频自由 | 久久精品网址 | 亚洲精品av在线 | 国产精品性夜天天视频 | 欧美一级爱操视频 | 动漫孕妇被羞羞视频 | a视频在线免费观看 | 免费一级在线视频 | 国产一级毛片国产 | 污黄视频在线观看 | 国产免费让你躁在线视频 | 双性精h调教灌尿打屁股的文案 | a一级黄色大片 | 久久精品视频12 | 九九热视频在线 | 成人短视频在线播放 | 精品一区二区三区在线观看视频 | 日韩 欧美 中文 | 护士xxxx | 免费一级欧美在线观看视频 |