最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue實(shí)現(xiàn)商品分類菜單數(shù)量提示功能

 更新時(shí)間:2019年07月26日 08:40:26   作者:前端大彬哥  
這篇文章主要介紹了Vue實(shí)戰(zhàn)—商品分類菜單數(shù)量提示功能,本文通過(guò)項(xiàng)目實(shí)戰(zhàn)給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

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

好先讓我回顧一下上節(jié)課的內(nèi)容,Goods組件,數(shù)量提示功能最終需要在Goods組件內(nèi)顯示。

<template>
 <div class="goods">
  <div class="menu-wrapper" ref="menuScroll">
   <ul>
    <!--專場(chǎng)-->
    <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>//通過(guò)i標(biāo)簽展示數(shù)量
    </li>
   </ul>
  </div>
  <!-- 右側(cè)商品列表 -->
  <div class="foods-wrapper" ref="foodScroll">
   <!--專場(chǎng)-->
   <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) {
     // 重點(diǎn)
     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)動(dòng)
      // 右->左
      // 計(jì)算區(qū)間
      this.caculateHeight();
     });
    }
   })
   .catch(function(error) {
    // handle error
    console.log(error);
   });
 },
 computed: {
  // 根據(jù)右側(cè)判斷左側(cè)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"
   );

   // 根據(jù)下標(biāo),滾動(dòng)到相對(duì)應(yīng)的元素
   let el = foodlist[index];
   // 滾動(dòng)到對(duì)應(yīng)元素的位置
   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函數(shù)實(shí)現(xiàn)計(jì)算個(gè)數(shù),數(shù)量來(lái)自于增減組件內(nèi)Cartcontrol。

calculateCount(spus) {
    console.log(spus)
   let count = 0;
   spus.forEach(food => {
    if (food.count > 0) {
     count += food.count;
    }
   });
   return count;
  },

以上是spus數(shù)據(jù)

Cartcontrol組件控制商品增減

通過(guò)組件Cartcontrol接受了來(lái)自父組件的傳值,并且我們?cè)诮M件內(nèi)添加商品的增減功能。

<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實(shí)例
    
  },
  increaseCart(){
   if(!this.food.count){
    Vue.set(this.food,'count',1);
   }else{
    this.food.count++;
   }
  }
  
 }
};
</script>

完善購(gòu)物車內(nèi)的數(shù)量統(tǒng)計(jì)

<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>
// 導(dǎo)入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: {
  // 總個(gè)數(shù)
  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;
  },
  // 結(jié)算按鈕顯示
  payStr() {
   if (this.totalCount > 0) {
    return "去結(jié)算";
   } else {
    return this.min_price_tip;
   }
  }
 },
 components: {
  BScroll
 }
};
</script>

現(xiàn)在商品分類菜單的數(shù)量提示就完成了。

總結(jié)

以上所述是小編給大家介紹的Vue實(shí)現(xiàn)商品分類菜單數(shù)量提示功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

  • 在vue-cli中組件通信的方法

    在vue-cli中組件通信的方法

    本篇文章主要介紹了在vue-cli中組件通信的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • vue 中使用print.js導(dǎo)出pdf操作

    vue 中使用print.js導(dǎo)出pdf操作

    這篇文章主要介紹了vue 中使用print.js導(dǎo)出pdf操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • vue和webpack打包項(xiàng)目相對(duì)路徑修改的方法

    vue和webpack打包項(xiàng)目相對(duì)路徑修改的方法

    這篇文章主要介紹了vue和webpack打包項(xiàng)目相對(duì)路徑修改的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • vue在手機(jī)中通過(guò)本機(jī)IP地址訪問(wèn)webApp的方法

    vue在手機(jī)中通過(guò)本機(jī)IP地址訪問(wèn)webApp的方法

    這篇文章主要介紹了vue在手機(jī)中通過(guò)本機(jī)IP地址訪問(wèn)webApp的方法,需要的朋友可以參考下
    2018-08-08
  • Vue實(shí)現(xiàn)兄弟組件間的聯(lián)動(dòng)效果

    Vue實(shí)現(xiàn)兄弟組件間的聯(lián)動(dòng)效果

    這篇文章主要介紹了Vue實(shí)現(xiàn)兄弟組件間的聯(lián)動(dòng)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-01-01
  • vue基礎(chǔ)之面包屑和標(biāo)簽tag詳解

    vue基礎(chǔ)之面包屑和標(biāo)簽tag詳解

    這篇文章主要為大家詳細(xì)介紹了vue的面包屑和標(biāo)簽tag,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • Element?UI中table單元格合并的解決過(guò)程

    Element?UI中table單元格合并的解決過(guò)程

    element?ui中的table表格數(shù)據(jù)是動(dòng)態(tài)生成的,最近遇到個(gè)需求,要求我們對(duì)單元格進(jìn)行合并,所以下面這篇文章主要給大家介紹了關(guān)于Element?UI中table單元格合并的解決過(guò)程,需要的朋友可以參考下
    2022-08-08
  • 詳解vuejs2.0 select 動(dòng)態(tài)綁定下拉框支持多選

    詳解vuejs2.0 select 動(dòng)態(tài)綁定下拉框支持多選

    這篇文章主要介紹了vuejs2.0 select動(dòng)態(tài)綁定下拉框 ,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 使用 Vue cli 3.0 構(gòu)建自定義組件庫(kù)的方法

    使用 Vue cli 3.0 構(gòu)建自定義組件庫(kù)的方法

    本文旨在給大家提供一種構(gòu)建一個(gè)完整 UI 庫(kù)腳手架的思路。通過(guò)實(shí)例代碼給大家講解了使用 Vue cli 3.0 構(gòu)建自定義組件庫(kù)的方法,感興趣的朋友跟隨小編一起看看吧
    2019-04-04
  • Vue路由組件傳遞參數(shù)的六種場(chǎng)景

    Vue路由組件傳遞參數(shù)的六種場(chǎng)景

    在Vue應(yīng)用程序中,路由組件是構(gòu)建單頁(yè)應(yīng)用的關(guān)鍵部分,傳遞參數(shù)給路由組件可以讓我們動(dòng)態(tài)地展示內(nèi)容,處理用戶輸入,以及實(shí)現(xiàn)各種交互功能,本文就給大家介紹了六種Vue路由組件傳遞參數(shù)場(chǎng)景,需要的朋友可以參考下
    2023-09-09

最新評(píng)論

黄梅县| 敦煌市| 南木林县| 宾阳县| 龙泉市| 桃园市| 肇州县| 江口县| 金昌市| 天水市| 格尔木市| 清镇市| 芜湖市| 金寨县| 安义县| 正定县| 旬邑县| 道孚县| 日喀则市| 屏边| 安陆市| 营口市| 景宁| 襄城县| 高密市| 专栏| 凤凰县| 伊金霍洛旗| 昭觉县| 翁牛特旗| 佛坪县| 雷州市| 绥滨县| 衡阳市| 富宁县| 涟水县| 平阴县| 云梦县| 洛扎县| 双江| 通化市|