自定义组件GoodsCard的封装以及使用(模态框用了elementplus)

84 阅读1分钟

GoodsCard组件页面代码

<template>
  <div class="goods-card-box">
    <div class="goods-card" v-for="(item, id) in list" :key="id">
      <div class="left">
        <image :src="item.img" mode="aspectFit"></image>
      </div>
      <div class="right">
        <div class="up">
          <div class="title">{{ item.name }}</div>
          <div class="price">{{ item.price_unit }} {{ item.price }}</div>
          <div class="total">
            <div class="">
              {{ item.total ? item.total : 0 }}{{ item.base_units }}(1:{{
                item.bigUnit.num ? item.bigUnit.num : 1
              }})
            </div>
            <div class="edit" @click="edit(item.id)">{{ item.editbtn }}</div>
          </div>
        </div>
        <div class="down">
          <div class="big" v-if="item.bigUnit.isBigUnit">
            <div class="jian leeicon" @click.prevent="subB(item.id)">-</div>
            <div class="num">
              {{ item.bigNum ? item.bigNum : 0 }}
            </div>
            <div class="jia leeicon" @click.prevent="addB(item.id)">+</div>
            <div class="unit">
              {{ item.bigUnit.title }}
            </div>
          </div>
          <div class="small">
            <div class="jian leeicon" @click.prevent="subS(item.id)">-</div>
            <div class="num">
              {{ item.smallNum ? item.smallNum : 0 }}
            </div>
            <div class="jia leeicon" @click.prevent="addS(item.id)">+</div>
            <div class="unit">{{ item.base_units }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "goods-card",
  props: ["list"],
  methods: {
    addB(id) {
      this.$emit("add-B", id);
    },
    subB(id) {
      this.$emit("sub-B", id);
    },
    addS(id) {
      this.$emit("add-S", id);
    },
    subS(id) {
      this.$emit("sub-S", id);
    },
    edit(id) {
      this.$emit("edit-n", id);
    },
  },
};
</script>
<style lang="less" scoped>
.goods-card {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  display: flex;
  margin-top: 45px;
  align-items: center;
  border-radius: 20px;
  background: #ffffff;
  box-shadow: 8px 8px 16px #d0d0d0, -8px -8px 16px #f0f0f0;
  .left {
    background-color: #ffc004;
    margin-left: 20px;
    border-radius: 20px;
    width: 100px;
    height: 100px;
    image {
      width: 100%;
      height: 100%;
      border-radius: 20px;
    }
  }
  .right {
    flex: 1;
    height: 100%;
    padding: 15px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .up {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      align-items: flex-start;
      .title {
        font-size: 34px;
      }
      .price {
        color: rgba(255, 74, 74, 1);
      }
      .total {
        color: #9e9e9e;
        display: flex;
        flex-direction: row;
        .edit {
          background-color: #ffc004;
          padding: 0 10px;
          color: white;
          border-radius: 25px;
          margin-left: 10px;
        }
      }
    }
    .down {
      display: flex;
      flex-direction: row;
      align-content: center;
      align-items: center;
      width: 100%;
      height: 40px;
      position: relative;
      padding-right: 5px;
      .big,
      .small {
        width: 50%;
        display: flex;
        flex-direction: row;
        justify-content: space-around;
        align-items: center;
        .num {
          color: #6b6b6b;
        }
        .unit {
          color: #6b6b6b;
        }
        .leeicon {
          width: 40px;
          height: 40px;
          line-height: 40px;
          border-radius: 50%;
          text-align: center;
          font-weight: bold;
          font-size: 23px;
          color: #ffbf00;
        }
      }
      .small {
        position: absolute;
        bottom: 0;
        right: 5px;
      }
    }
  }
}

</style>

使用页面代码,此处为App.vue

<template>

  <div>

    <div class="card">

      <goods-card

        :list="list"

        @add-B="addBB($event)"

        @sub-B="subBB($event)"

        @add-S="addSS($event)"

        @sub-S="subSS($event)"

        @edit-n="editNum($event)"

      ></goods-card>

    </div>

    <el-dialog v-model="isShowModal" title="修改数量" width="30%" center>

      <el-input

        v-model="total"

        @input="inputNum"

        type="number"

        placeholder="最大为5位数"

        maxlength="5"

      />

      <template #footer>

        <span class="dialog-footer">

          <el-button @click="cancelAdd">取消</el-button>

          <el-button type="primary" @click="confirmAdd"> 保存 </el-button>

        </span>

      </template>

    </el-dialog>

  </div>

</template>

  


<script>

import GoodsCard from "@/myComponents/GoodsCard.vue";

export default {

  name: "App",

  components: {

    GoodsCard,

  },

  data() {

    return {

      isShowModal: false,

      total: 0,

      list: [

        {

          id: 1,

          img: require("./assets/logo.png"),

          name: "名称", //名称

          price: 100, //价格

          price_unit: "$", //价格单位

          total: 0, //总数

          base_units: "瓶", //基础单位

          editbtn: "编辑", //编辑或者修改数量

          bigUnit: {

            isBigUnit: true, //存在大单位

            title: "箱", //大单位

            num: 6, //大单位与小单位比值

          },

          bigNum: 0, //大单位数量

          smallNum: 0, //小单位数量

        },

        {

          id: 2,

          img: require("./assets/logo.png"),

          name: "名称2", //名称

          price: 100, //价格

          price_unit: "$", //价格单位

          total: 0, //总数

          base_units: "瓶", //基础单位

          editbtn: "编辑", //编辑或者修改数量

          bigUnit: {

            isBigUnit: true, //存在大单位

            title: "箱", //大单位

            num: 6, //大单位与小单位比值

          },

          bigNum: 0, //大单位数量

          smallNum: 0, //小单位数量

        },

        {

          id: 3,

          img: require("./assets/logo.png"),

          name: "名称2", //名称

          price: 100, //价格

          price_unit: "$", //价格单位

          total: 0, //总数

          base_units: "瓶", //基础单位

          editbtn: "编辑", //编辑或者修改数量

          bigUnit: {

            isBigUnit: false, //存在大单位

            title: "", //大单位

            num: 1, //大单位与小单位比值

          },

          bigNum: 0, //大单位数量

          smallNum: 0, //小单位数量

        },

      ],

    };

  },

  methods: {

    //大单位数量加

    addBB(event) {

      console.log(event, "event");

      this.list.some((item) => {

        if (item.id == event) {

          item.bigNum++;

          item.total = item.bigNum * item.bigUnit.num + item.smallNum;

        }

      });

    },

    //大单位数量减

    subBB(event) {

      console.log(event, "event");

      this.list.some((item) => {

        if (item.id == event && item.bigNum > 0) {

          item.bigNum--;

          item.total = item.bigNum * item.bigUnit.num + item.smallNum;

        }

      });

    },

    //小单位数量加

    addSS(event) {

      console.log(event, "event");

      this.list.some((item) => {

        if (item.id == event) {

          item.total++;

          item.bigNum = parseInt(item.total / item.bigUnit.num);

          item.smallNum = item.total % item.bigUnit.num;

        }

      });

    },

    //小单位数量减

    subSS(event) {

      console.log(event, "event");

      this.list.some((item) => {

        if (item.id == event && item.total > 0) {

          item.total--;

          item.smallNum = item.total % item.bigUnit.num;

          item.bigNum = parseInt(item.total / item.bigUnit.num);

        }

      });

    },

    //打开模态框编辑数量,这里的模态框用的是elementplus,也可以自定义或者用其他组件的。

    editNum(event) {

      console.log(event, "event");

      this.isShowModal = true;

      this.list.some((item) => {

        if (item.id == event) {

          localStorage.setItem("goodsId", item.id); //在全局或者是data中把编辑数量时的id存起来

          if (item.total) {

            this.total = item.total;

          } else {

            this.total = 0;

          }

        }

      });

    },

    //模态框取消按钮

    cancelAdd(event) {

      console.log(event, "event");

      this.isShowModal = false;

    },

    //模态框保存按钮

    confirmAdd(event) {

      console.log(event, "event");

      let e = localStorage.getItem("goodsId"); //取出之前存的id

      this.list.some((item) => {

        if (item.id == e) {

          item.total = Number(this.total);

          item.smallNum = item.total % item.bigUnit.num;

          item.bigNum = parseInt(item.total / item.bigUnit.num);

        }

      });

      this.isShowModal = false;

    },

    //模态框输入

    inputNum(event) {

      console.log(event, "event");

      this.total = event; //elementui,获取输入值

    },

  },

};

</script>

  


<style scoped lang="less">

.card {

  width: 500px;

  height: 300px;

}

</style>