编写复用组件card

模拟淘宝中的商品组件

 

 

组件代码

<template>
  <div class="card" :style="width ? { width: width + 'px' } : {}">
    <div class="img">
      <img :src="imgSrc" :style="imgStyle ? imgStyle : {}" />
    </div>
    <div v-if="activeContext" class="main">
      <span
        class="activeLabel"
        v-if="activeLabel"
        :style="activeLabelStyle ? activeLabelStyle : {}"
      >
        {{ activeLabel }}
      </span>
      <span>{{ activeContext }}</span>
    </div>
    <div v-else class="main"><slot name="main"></slot></div>
    <div v-if="price" class="footer">
      <span class="money">¥</span>{{ prices }}
    </div>
    <div v-else><slot name="footer"></slot></div>
  </div>
</template>
<script>
export default {
  name: "card",
  props: {
    width: {
      type: Number,
    },
    imgStyle: {
      type: Object,
      default: () => {},
    },
    imgSrc: {
      type: String,
    },
    activeContext: {
      type: String,
      default: "",
    },
    activeLabel: {
      type: String,
      default: "",
    },
    activeLabelStyle: {
      type: Object,
      default: () => {},
    },
    price: {
      type: Number,
      default: 0,
    },
  },

  computed: {
    // 格式化价格
    prices: function () {
      let source = String(this.price.toFixed(2));
      let valueStr = source.toString();
      return valueStr.replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, "$1,");
    },
  },
};
</script>

使用方法

<template>
  <div id="app">
    <card v-bind="cardConfig"></card>
    <card v-bind="cardConfig3" style="margin-top: 10px"></card>
    <card v-bind="cardConfig2" style="margin-top: 10px">
      <template v-slot:main>
        <h4 class="h4">花开盆上新韩国花盆</h4>
        <p>
          多肉花盆,立体玫瑰浮雕,田园风格十足。手工制作,经过做旧处理,会有一些微斑瑕疵等等,更加显露出宝贝的淳朴风格。
        </p>
      </template>
    </card>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      cardConfig: {
        imgSrc: "板凳.webp",
        activeContext:
          "正宗越南铁木砧板菜板实木家用整木圆形抗菌防霉案板厨房切菜板",
        activeLabel: "造物记",
        price: 203213216.5464654,
      },
      cardConfig3: {
        imgSrc: "摆设.webp",
        activeContext:
          "黑檀实木茶杯垫 杯托茶杯架隔热垫碗垫功夫茶具垫茶托组圆形6片装",
        activeLabel: "新年季",
        activeLabelStyle:{
          color:"white",
          backgroundColor:"red"
        },
        price: 216.554,
      },
      cardConfig2: {
        imgSrc: "盆栽.webp",
      },
    };
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

效果图

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值