Browse Source

数字支持字符串显示

master
buaixuexideshitongxue 3 weeks ago
parent
commit
b53fdde183
  1. 63
      src/components/datav/statistic.vue

63
src/components/datav/statistic.vue

@ -1,52 +1,57 @@
<template>
<div class="statistic">
<div class="statistic-number">
{{ isDecimal ? parseFloat(outputValue.toFixed(2)) : parseInt(outputValue) }}{{ valueUnit }}
<!-- 如果是字符串则直接显示否则显示动画数字 + 单位 -->
<template v-if="typeof props.value === 'string'">
{{ props.value }}
</template>
<template v-else>
{{ formattedNumber }}{{ valueUnit }}
</template>
</div>
<div class="statistic-title" :title="tooltip">{{ title }}</div>
</div>
</template>
<script setup>
import {useTransition} from "@vueuse/core";
import {computed, ref, watch} from 'vue';
const props = defineProps({
tooltip: {
type: String,
default: '',
},
title: {
type: String
},
value: {
type: Number,
default: 0,
},
valueUnit: {
type: String,
},
//
isDecimal: {
type: Boolean,
default: false
}
tooltip: {type: String, default: ''},
title: {type: String},
// Number String
value: {type: [Number, String], default: 0},
valueUnit: {type: String},
isDecimal: {type: Boolean, default: false}
});
const value = ref(props.value);
console.log(
value
)
// value
const numericValue = ref(typeof props.value === 'number' ? props.value : 0);
watch(() => props.value, (val) => {
value.value = val || 0;
if (typeof val === 'number') {
numericValue.value = val || 0;
}
// numericValue使
});
//
const animatedValue = useTransition(numericValue, {duration: 1500});
})
const outputValue = useTransition(value, {
duration: 1500,
//
const formattedNumber = computed(() => {
if (typeof props.value === 'string') return '';
const num = animatedValue.value;
return props.isDecimal ? parseFloat(num.toFixed(2)) : parseInt(num);
});
</script>
<style lang="scss" scoped>
.statistic {
text-align: center;
cursor: pointer;
.statistic-number {
font-size: 39px;
font-weight: 700;
@ -85,4 +90,4 @@ const outputValue = useTransition(value, {
font-size: 27px;
}
}
</style>
</style>
Loading…
Cancel
Save