在VUE中使用countup.js

在iview admin中使用countup报错,检查了下代码发现iview admin使用的是countup.js 1.8.2,“Published 3 years ago”
于是重新安装了新版vue-countup-v2

1
npm install --save countup.js vue-countup-v2

调整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<template>
    <div class="count-to-wrapper">
        <slot name="left"/>
            <p class="content-outer">
                <ICountUp :class="['count-to-count-text', countClass]" :delay="delay" :endVal="end" :options="options" @ready="onReady"/>
            </p>
        <slot name="right"/>
    </div>
</template>

<script>
import ICountUp from 'vue-countup-v2'
import './index.less'
export default {
    name: 'CountTo',
    components: {
        ICountUp
    },
    props: {
        /**
         * @description 结束值,即动画结束后显示的数值
         */
        end: {
            type: Number,
            required: true
        },
        /**
         * @description 动画延迟开始的时间,单位是秒
         */
        delay: {
            type: Number,
            default: 0
        },
        // options
        /**
         * @description 起始值,即动画开始前显示的数值
         */
        startVal: {
            type: Number,
            default: 0
        },
        /**
         * @description 保留几位小数
         */
        decimals: {
            type: Number,
            default: 0
        },
        /**
         * @description 动画持续的时间,单位是秒
         */
        duration: {
            type: Number,
            default: 2
        },
        /**
         * @description 是否使用分组,分组后每三位会用一个符号分隔
         */
        usegroup: {
            type: Boolean,
            default: true
        },
        /**
         * @description 是否禁用easing动画效果
         */
        uneasing: {
            type: Boolean,
            default: false
        },
        /**
         * @description 用于分组(usegroup)的符号
         */
        separator: {
            type: String,
            default: ','
        },
        /**
         * @description 分隔整数和小数的符号,默认是小数点
         */
        decimal: {
            type: String,
            default: '.'
        },
        // simplify
        /**
         * @description 是否简化显示,设为true后会使用unit单位来做相关省略
         */
        simplify: {
            type: Boolean,
            default: true
        },
        /**
         * @description 自定义单位,如[3, 'K+'], [6, 'M+']即大于3位数小于6位数的用k+来做省略
         *              1000即显示为1K+
         */
        unit: {
            type: Array,
            default () {
                return [[3, 'K+'], [6, 'M+'], [9, 'B+']]
            }
        },
        countClass: {
            type: String,
            default: ''
        },
        unitClass: {
            type: String,
            default: ''
        }
    },
    data () {
        return {
            options: {
                startVal: this.startVal, // number to start at (0)
                decimalPlaces: this.decimals, // number of decimal places (0)
                duration: this.duration, // animation duration in seconds (2)
                useGrouping: this.usegroup, // example: 1,000 vs 1000 (true)
                useEasing: !this.uneasing, // ease animation (true)
                smartEasingThreshold: 999, // smooth easing for large numbers above this if useEasing (999)
                smartEasingAmount: 333, // amount to be eased for numbers above threshold (333)
                separator: this.separator, // grouping separator (',')
                decimal: this.decimal, // decimal ('.')
                // easingFn: easing function for animation (easeOutExpo)
                // easingFn: (t: number, b: number, c: number, d: number) => number;
                // formattingFn: (n: number) => string; // this function formats result
                formattingFn: this.getValue,
                prefix: '', // text prepended to result
                suffix: ''// text appended to result
                // numerals: string[]; // numeral glyph substitution
            },
            unitText: ''
        }
    },
    methods: {
        // 完成
        onReady: function(instance, CountUp) {
            // const that = this;
            // instance.update(that.endVal + 1000);
        },
        getHandleVal (val, len) {
            return {
                endVal: parseInt(val / Math.pow(10, this.unit[len - 1][0])),
                unitText: this.unit[len - 1][1]
            }
        },
        transformValue (val) {
            const len = this.unit.length
            let res = {
                endVal: 0,
                unitText: ''
            }
            if (val < Math.pow(10, this.unit[0][0])) res.endVal = val
            else {
                for (let i = 1; i < len; i++) {
                if (val >= Math.pow(10, this.unit[i - 1][0]) && val < Math.pow(10, this.unit[i][0])) res = this.getHandleVal(val, i)
                }
            }
            if (val > Math.pow(10, this.unit[len - 1][0])) res = this.getHandleVal(val, len)
            return res
        },
        getValue (val) {
            let res = 0
            if (this.simplify) {
                const { endVal, unitText } = this.transformValue(val)
                this.unitText = unitText
                res = endVal + '<i class="' + this.unitClass + '">' + unitText + '</i>'
            } else {
                res = val
            }
            return res
        }
    }
}
</script>