注:组件中使用到的icon为svg通过icomoon.io转换而来的css icon,实际使用需要换成你的icon

使用例子如下:

            <li @click="selectFood(food,$event)" v-for="food in item.foods" class="food-item border-1px">
              <div class="icon">
                <img width="57" height="57" :src="food.icon">
              </div>
              <div class="content">
                <h2 class="name">{{food.name}}</h2>
                <div class="extra">
                  <p class="desc">{{food.description}}</p>
                  <span class="count">月售{{food.sellCount}}份</span><span>好评率{{food.rating}}%</span>
                </div>
                <div class="price">
                  <span class="now">¥{{food.price}}</span><span class="old" v-if="food.oldPrice">{{food.oldPrice}}</span>
                </div>
                <div class="cartcontrol-wrapper">
                  <cartcontrol :food="food"></cartcontrol>
                </div>
              </div>
            </li>

其中,调用控制按钮组件的代码为:

                <div class="cartcontrol-wrapper">
                  <cartcontrol :food="food"></cartcontrol>
                </div>

例子运行效果,箭头处为控制按钮组件:


以下为组件代码

template:

icon-remove_circle_outline 为减号icon对应的类,为svg通过icomoon.io生成。icon-add_circle为加号icon对应的类

<template>
  <div class="cartcontrol">
    <transition name="move">
      <div class="cart-decrease" v-if="food.count>0"@click.stop.prevent="decreaseFood">
        <span class="inner icon-remove_circle_outline"></span>
      </div>
    </transition>
    <div class="cart-count" v-if="food.count>0">{{this.food.count}}</div>
    <div class="cart-add icon-add_circle" @click.stop.prevent="addFood"></div>
  </div>
</template>

JavaScript:

<script type="text/ecmascript-6">
  import Vue from 'vue';
  export default {
    props: {
      food: {
        type: Object
      }
    },
    methods: {
      addFood(event) {
        if (!event._constructed) {
          return;
        }
        if (!this.food.count) {
          Vue.set(this.food, 'count', 1);
        } else {
          this.food.count++;
        }
      },
      decreaseFood(event) {
        if (!event._constructed) {
          return;
        }
        if (this.food.count) {
          this.food.count--;
        }
      }
    }
  };
</script>

stylus:(功能类似于sass、less)

<style lang="stylus" rel="stylus/stylesheet">
  .cartcontrol
    font-size 0
    .cart-decrease
      display inline-block
      padding 6px
      transition all 0.4s linear
      &.move-enter-active,&.move-leave
        opacity 1
        transform translate3D(0,0,0)
      .inner
        display block
        font-size 24px
        line-height 24px
        color rgb(0, 160, 220)
        transition all 0.4s linear
        transform rotate(0)
      &.move-enter,&.move-leave-active
        opacity 0
        transform translate3D(24px,0,0)
        .inner
          transform rotate(180deg)
    .cart-add
      display inline-block
      padding 6px
      font-size 24px
      line-height 24px
      color rgb(0, 160, 220)
    .cart-count
      display inline-block
      vertical-align top
      width 12px
      padding-top 6px
      line-height 24px
      text-align center
      font-size 10px
      color rgb(147, 153, 159)


</style>



Logo

前往低代码交流专区

更多推荐