vue3修改带小数点的价格数字:小数点的前后数字,要分别显示成不同颜色和大小!已经封装成组件了!

需求:

修改带小数点的价格数字:小数点的前后数字,要分别显示成不同颜色和大小!已经封装成组件了!

效果:

前面大,后面小

代码:

组件:

<!--修改小数点前后数字不同颜色和大小的组件-->
<template>
  <view class="price-container">
    <text class="current-price">
     <text class="price-icon"> ¥</text>
      <!-- 显示小数点前的价格部分,应用传入的颜色和字体大小 -->
      <text :style="{ color: integerColor, fontSize: integerFontSize }" class="price-integer">
        {{ integerPart }}
      </text>
      <text class="dot">.</text>
      <!-- 显示小数点后的价格部分,应用传入的颜色和字体大小 -->
      <text :style="{ color: decimalColor, fontSize: decimalFontSize }" class="price-decimal">
        {{ decimalPart }}
      </text>
    </text>
  </view>
</template>

<script setup>
// 导入所需的 Vue API
import { computed, defineProps } from 'vue';

// 定义组件的 props
const props = defineProps({
  // 价格值,必填项
  price: {
    type: Number,
    required: true
  },
  // 整数部分字体颜色,默认黑色
  integerColor: {
    type: String,
    default: '#000000'
  },
  // 整数部分字体大小,默认 24px
  integerFontSize: {
    type: String,
    default: '24px'
  },
  // 小数部分字体颜色,默认黑色
  decimalColor: {
    type: String,
    default: '#000000'
  },
  // 小数部分字体大小,默认 16px
  decimalFontSize: {
    type: String,
    default: '16px'
  }
});

// 确保 price 是一个有效的数字
const numericPrice = computed(() => {
  return Number(props.price) || 0;
});

// 计算价格的整数部分
const integerPart = computed(() => {
  const priceStr = numericPrice.value.toFixed(2); // 保证小数点后有两位
  return priceStr.split('.')[0]; // 获取小数点前的部分
});

// 计算价格的小数部分
const decimalPart = computed(() => {
  const priceStr = numericPrice.value.toFixed(2); // 保证小数点后有两位
  return priceStr.split('.')[1]; // 获取小数点后的部分
});

</script>

<style lang="scss" scoped>
/* 定义价格容器的布局 */
.price-container {
  display: flex;
  align-items: baseline; /* 对齐基线,使得不同字体大小的文本对齐 */
}

/* 定义当前价格的样式 */
.current-price {
  display: flex;
  align-items: baseline; /* 对齐基线,使得不同字体大小的文本对齐 */
  .price-icon{
    font-size: 24rpx;
    margin-right: 2rpx;
  }
  .dot{
    color: #333333;
  }
}

/* 前面的整数,默认样式可以在这里定义 */
.price-integer {
  /* 这里可以设置默认样式 */
  color: #333333;

}

//后面的小数点
.price-decimal {
  /* 这里可以设置默认样式 */
  color: #333333;
}
</style>

使用组件:

灰色:

     <PriceDisplay
              :price="item.price"
              integerColor="#333333"
              integerFontSize="36rpx"
              decimalColor="#333333"
              decimalFontSize="24rpx"
              class="priceContainerWrapper"
          />

彩色:

   <PriceDisplay
              :price="123.45"
              integerColor="#FF5733"
              integerFontSize="30px"
              decimalColor="#33CFFF"
              decimalFontSize="18px"
          />

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南北极之间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值