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>

效果展示

相关推荐
易知微EasyV数据可视化1 分钟前
Web+游戏引擎模式:设计的跨界协同最优解 | 数字孪生实战训练营·设计篇
前端·经验分享·游戏引擎·数字孪生·空间智能
羊羊小栈6 分钟前
农业病害知识管理系统(基于前后端Web开发)
前端·人工智能·毕业设计·大作业
武子康8 分钟前
调查研究-156 Vercel 全栈应用 前端零配置极速上线:Serverless + 边缘网络 + CI/CD 全栈实战
前端·网络·ci/cd·ai·云原生·serverless·vecel
码云骑士10 分钟前
Chrome插件开发实战指南:从零到上架
前端·chrome·microsoft
kiritomzzz14 分钟前
Vue 插槽(Slot)全解析:从 Vue2 到 Vue3 核心用法与案例
前端·javascript·vue.js
喵了几个咪15 分钟前
基于 Nuxt 4 的现代 Headless CMS 前端:架构深度解析与二次开发指南
前端·架构
weixin_427771611 小时前
css加载顺序导致本地和线上样式不一致
前端·css
漂流瓶jz8 小时前
Webpack如何实现万物皆可import?loader的使用/配置/手写实践
前端·javascript·webpack
ZC跨境爬虫9 小时前
跟着 MDN 学CSS day_41:显式轨道、隐式网格与区域命名放置
前端·javascript·css·ui·交互