uniapp 金额处理组件

html 复制代码
<template>
	<view :class="{ line: line, weight: weight }" :style="{ color: color, fontFamily: SemiBold ? 'SemiBold' : 'Regular' }" class="base-money">
		<text v-show="preFix" class="preFix" :style="{ 'font-size': preFixSize + 'rpx', color: textColor }">{{ preFix }}</text>
		<text class="symbol" :style="{ 'font-size': symbolSize + 'rpx' }">¥</text>
		<text class="integer" :style="{ 'font-size': integerSize + 'rpx' }">{{ integer }}</text>
		<text v-if="digits && showDigits" class="decimal" :style="{ 'font-size': decimalSize + 'rpx' }">.{{ decimal }}</text>
	</view>
</template>

<script>
export default {
	name: 'BaseMoney',
	props: {
		// 小数位数,为0则不显示
		digits: {
			type: Number,
			default: 2
		},
		money: {
			type: String | Number,
			default: ''
		},
		// 删除线
		line: {
			type: Boolean,
			default: false
		},
		// 粗体
		weight: {
			type: Boolean,
			default: false
		},
		color: {
			type: String,
			default: '#f50'
		},
		textColor: {
			type: String,
			default: '#999'
		},
		symbolSize: {
			type: String,
			default: '20'
		},
		integerSize: {
			type: String,
			default: '26'
		},
		decimalSize: {
			type: String,
			default: '24'
		},
		inline: {
			type: Boolean,
			default: false
		},
		preFix: {
			type: String,
			default: ''
		},
		preFixSize: {
			type: String,
			default: '24'
		},
		SemiBold: {
			type: Boolean,
			default: true
		},
		isCoupon: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {
			integer: 0,
			decimal: 0,
			showDigits: false
		};
	},
	watch: {
		money: {
			handler(newValue, oldValue) {
				let value = Number(newValue).toFixed(this.digits);
				value = value.split('.');
				this.integer = value[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
				if (value[1]) {
					this.decimal = value[1];
					if ((this.isCoupon && this.decimal != '00') || !this.isCoupon) {
						this.showDigits = true;
					}
				}
			},
			immediate: true
		}
	}
};
</script>

<style lang="scss" scoped>
.base-money {
	display: inline-block;
	&.line {
		text-decoration: line-through;
	}

	&.weight {
		font-weight: 500;
	}
}
.preFix {
	font-weight: 500 !important;
	font-family: PingFang SC-Medium, PingFang SC !important;
}
.SemiBold {
	font-family: 'SemiBold';
}
.Regular {
	font-family: 'Regular';
}
</style>

效果展示

相关推荐
IT_陈寒2 分钟前
Vue这个坑我跳了两次,原来问题出在这
前端·人工智能·后端
kyriewen5 分钟前
我用 50 行代码重写了 React Router 核心,终于搞懂了前端路由原理
前端·javascript·react.js
WebInfra1 小时前
Rspack 2.1 发布:React Compiler 提速 10 倍!
前端
李明卫杭州1 小时前
CSS 媒体查询详解:一文掌握响应式设计的核心技术
前端
lichenyang4532 小时前
从 H5 按钮到 OpenHarmony 能力调用:我如何理解 ASCF 的运行链路
前端
下家2 小时前
我放弃了 Vue/React,选择自研框架
前端·前端框架
Asize3 小时前
HTML5 Canvas 基础:从按帧动画到 ECharts 数据可视化
前端·javascript·canvas
默_笙3 小时前
🎄 后端给我一堆扁平数据,我 10 行代码把它变成了树
前端·javascript
Mahut3 小时前
我用 Electron + FFmpeg 做了一个本地视频处理工作站 ClipForge
前端·ffmpeg·electron
前端Hardy3 小时前
又一个 AI 神器火了!
前端·javascript·后端