Procházet zdrojové kódy

feat:新增数字滚动组件动态获取数据功能

奔跑的面条 před 3 roky
rodič
revize
bcd1dfd7bd

+ 32 - 12
src/packages/components/Decorates/Mores/Number/index.vue

@@ -6,13 +6,8 @@
       </span>
     </template>
     <span :style="`color:${numberColor};font-size:${numberSize}px`">
-      <n-number-animation
-        :from="from"
-        :to="to"
-        :duration="dur * 1000"
-        :show-separator="showSeparator"
-        :precision="precision"
-      ></n-number-animation>
+      <n-number-animation :from="option.from" :to="option.to" :duration="dur * 1000" :show-separator="showSeparator"
+        :precision="precision"></n-number-animation>
     </span>
     <template #suffix>
       <span :style="`color:${suffixColor};font-size:${numberSize}px`">
@@ -23,8 +18,10 @@
 </template>
 
 <script setup lang="ts">
-import { PropType, toRefs } from 'vue'
+import { PropType, toRefs, ref, reactive, watch } from 'vue'
 import { CreateComponentType } from '@/packages/index.d'
+import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
+import { useChartDataFetch } from '@/hooks'
 
 const props = defineProps({
   chartConfig: {
@@ -32,21 +29,44 @@ const props = defineProps({
     required: true,
   },
 })
-
+const option = reactive({
+  from: 0,
+  to: 0,
+})
 const { w, h } = toRefs(props.chartConfig.attr)
-const {
+let {
   dur,
   showSeparator,
   prefixText,
   prefixColor,
   suffixText,
   suffixColor,
-  from,
-  to,
   precision,
   numberSize,
   numberColor,
 } = toRefs(props.chartConfig.option)
+
+const updateNumber = (newData: number) => {
+  // 原来的目标值作为新的数字动画的起始值
+  option.from = option.to
+  option.to = newData
+}
+
+watch(
+  () => props.chartConfig.option.from,
+  () => {
+    option.from = props.chartConfig.option.from
+  }, { immediate: true }
+)
+
+watch(
+  () => props.chartConfig.option.to,
+  () => {
+    option.to = props.chartConfig.option.to
+  }, { immediate: true }
+)
+
+useChartDataFetch(props.chartConfig, useChartEditStore, updateNumber)
 </script>
 <style lang="scss" scoped>
 @include go('decorates-number') {