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_陈寒1 分钟前
Vite 5年迭代揭秘:3个核心优化让你的项目构建速度提升200%
前端·人工智能·后端
2501_9151063211 分钟前
iOS 可分发是已经上架了吗?深入解析应用分发状态、ipa 文件上传、TestFlight 测试与 App Store 审核流程
android·ios·小程序·https·uni-app·iphone·webview
怎么吃不饱捏17 分钟前
vue3+vite,引入阿里巴巴svg图标,自定义大小颜色
前端·javascript·vue.js
无敌最俊朗@18 分钟前
MQTT 关键特性详解
java·前端·物联网
JAVA学习通19 分钟前
微服务项目->在线oj系统(Java-Spring)----[前端]
java·开发语言·前端
excel23 分钟前
Vue3 响应式系统核心执行器:Reactive Effect 与依赖调度机制
前端
南玖i2 小时前
vue3 通过 Vue3DraggableResizable实现拖拽弹窗,可修改大小
前端·javascript·vue.js
excel2 小时前
Web发展与Vue.js导读
前端
YAY_tyy2 小时前
Three.js 开发实战教程(五):外部 3D 模型加载与优化实战
前端·javascript·3d·three.js
Zuckjet_5 小时前
开启 3D 之旅 - 你的第一个 WebGL 三角形
前端·javascript·3d·webgl