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>

效果展示

相关推荐
JosieBook40 分钟前
【web应用】如何进行前后端调试Debug? + 前端JavaScript调试Debug?
前端·chrome·debug
LBJ辉40 分钟前
2. Webpack 高级配置
前端·javascript·webpack
灵感__idea7 小时前
JavaScript高级程序设计(第5版):好的编程就是掌控感
前端·javascript·程序员
烛阴8 小时前
Mix
前端·webgl
代码续发8 小时前
前端组件梳理
前端
试图让你心动9 小时前
原生input添加删除图标类似vue里面移入显示删除[jquery]
前端·vue.js·jquery
陈不知代码10 小时前
uniapp创建vue3+ts+pinia+sass项目
前端·uni-app·sass
小王码农记10 小时前
sass中@mixin与 @include
前端·sass
源码_V_saaskw10 小时前
JAVA图文短视频交友+自营商城系统源码支持小程序+Android+IOS+H5
java·微信小程序·小程序·uni-app·音视频·交友
陈琦鹏10 小时前
轻松管理 WebSocket 连接!easy-websocket-client
前端·vue.js·websocket