从数字渐变组件(countUp.js)谈第三方JS库的使用
1、组件封装基础
实例
父组件count-to.vue:
<template>
<div>
// 父组件获取DOM通过ref属性,父组件传递给子组件end-val属性
<count-to ref="countTo" :end-val="endVal" @on-animation-end="handleEnd">
<span slot="left">总金额: </span>
<span slot="right">元</span>
</count-to>
<button @click="getNumber">获取数值</button>
<button @click="up">更新值</button>
</div>
</template>
<script>
import CountTo from '@/components/count-to'
export default {
name: 'count_to',
components: {
CountTo
},
data () {
return {
endVal: 100
}
},
methods: {
getNumber () {
this.$refs.countTo.getCount()
},
up () {
this.endVal += Math.random() * 100
},
handleEnd (endVal) {
console.log('end -> ', endVal)
}
}
}
</script>
子组件count-to.vue:
<template>
<div>
<slot name="left"></slot>
<span ref="number" :class="countClass" :id="eleId"></span>
<slot name="right"></slot>
</div>
</template>
<script>
import CountUp from 'countup'
export default {
name: 'CountTo',
computed: {
eleId () {
// 当组件编译时会生成当前编译实例的id
return `count_up_${this._uid}`
},
countClass () {
return [
'count-to-number',
this.className
]
}
},
data () {
return {
counter: {}
}
},
// 接收父组件传递的参数
props: {
/**
* @description 起始值
*/
startVal: {
type: Number,
default: 0
},
/**
* @description 最终值
*/
endVal: {
type: Number,
required: true
},
/**
* @description 小数点后保留几位小数
*/
decimals: {
type: Number,
default: 0
},
/**
* @description 动画延迟开始时间
*/
delay: {
type: Number,
default: 0
},
/**
* @description 渐变时长
*/
duration: {
type: Number,
default: 1
},
/**
* @description 是否使用变速效果
*/
useEasing: {
type: Boolean,
default: false
},
/**
* @description 是否使用变速效果
*/
useGrouping: {
type: Boolean,
default: true
},
/**
* @description 分组符号
*/
separator: {
type: String,
default: ','
},
/**
* @description 整数和小数分割符号
*/
decimal: {
type: String,
default: '.'
},
className: {
type: String,
default: ''
}
},
methods: {
getCount () {
return this.$refs.number.innerText
},
emitEndEvent () {
setTimeout(() => {
this.$nextTick(() => {
// this.$emit调用父组件绑定的函数
this.$emit('on-animation-end', Number(this.getCount()))
})
}, this.duration * 1000 + 5)
}
},
watch: {
endVal (newVal, oldVal) {
this.counter.update(newVal)
this.emitEndEvent()
}
},
mounted () {
this.$nextTick(() => {
// 这个是实例化CountUp插件
this.counter = new CountUp(this.eleId, this.startVal, this.endVal, this.decimals, this.duration, {
useEasing: this.useEasing,
useGrouping: this.useGrouping,
separator: this.separator,
decimal: this.decimal
})
setTimeout(() => {
this.counter.start()
this.emitEndEvent()
}, this.delay)
})
}
}
</script>
<style lang="less">
@import './count-to.less';
</style>
2、组件中使用id值
从上面代码可以看出每次编译Vue组件的时候都会生成_uid标识符,并且这个_uid在相同组件的不同实例中都不一样
3、组件中获取DOM
在组件上添加ref属性,获取DOM的时候用this.$refs