vue3使用vue-count-to組件的實(shí)現(xiàn)
項(xiàng)目場(chǎng)景:
數(shù)據(jù)可視化大屏開發(fā)的過程中,需要實(shí)現(xiàn)一種滾動(dòng)數(shù)字的效果,在使用vue2時(shí),使用vue-count-to完全沒有問題,功能也比較完善(滾動(dòng)時(shí)長,開始值,結(jié)束值,前綴,后綴,千分隔符,小數(shù)分隔符等等),但是在vue3中使用會(huì)出現(xiàn)問題。
<template> <div id="nav"> <router-link to="/">Home</router-link> | <router-link to="/about">About</router-link> </div> <count-to :startVal="0" :endVal="2045" :duration="4000"></count-to> <router-view/> </template>
展示的效果
問題描述:
出現(xiàn)的錯(cuò)誤時(shí) == Cannot read property ‘_c' of undefined== 這是一個(gè)_c的屬性沒有找到,具體的情況也不是很清楚。在vue-count-to打包后的源碼中可以大致看出來,這是在render函數(shù)中出現(xiàn)的錯(cuò)誤。但是還是沒法下手。
解決方案:
采用的方法是直接復(fù)制node_modules下vue-count-to的源文件(src下),到自己項(xiàng)目的components下。如圖
然后根據(jù)eslint的檢查,修改代碼,直到不報(bào)錯(cuò),且記刪除package.json下剛剛引入的vue-count-to的依賴。如圖
最后重啟項(xiàng)目。
vue-count-to源碼
let lastTime = 0 const prefixes = 'webkit moz ms o'.split(' ') // 各瀏覽器前綴 let requestAnimationFrame let cancelAnimationFrame const isServer = typeof window === 'undefined' if (isServer) { requestAnimationFrame = function () { } cancelAnimationFrame = function () { } } else { requestAnimationFrame = window.requestAnimationFrame cancelAnimationFrame = window.cancelAnimationFrame let prefix // 通過遍歷各瀏覽器前綴,來得到requestAnimationFrame和cancelAnimationFrame在當(dāng)前瀏覽器的實(shí)現(xiàn)形式 for (let i = 0; i < prefixes.length; i++) { if (requestAnimationFrame && cancelAnimationFrame) { break } prefix = prefixes[i] requestAnimationFrame = requestAnimationFrame || window[prefix + 'RequestAnimationFrame'] cancelAnimationFrame = cancelAnimationFrame || window[prefix + 'CancelAnimationFrame'] || window[prefix + 'CancelRequestAnimationFrame'] } // 如果當(dāng)前瀏覽器不支持requestAnimationFrame和cancelAnimationFrame,則會(huì)退到setTimeout if (!requestAnimationFrame || !cancelAnimationFrame) { requestAnimationFrame = function (callback) { const currTime = new Date().getTime() // 為了使setTimteout的盡可能的接近每秒60幀的效果 const timeToCall = Math.max(0, 16 - (currTime - lastTime)) const id = window.setTimeout(() => { const time = currTime + timeToCall callback(time) }, timeToCall) lastTime = currTime + timeToCall return id } cancelAnimationFrame = function (id) { window.clearTimeout(id) } } } export { requestAnimationFrame, cancelAnimationFrame }
<template> <span> {{displayValue}} </span> </template> <script> import { requestAnimationFrame, cancelAnimationFrame } from './requestAnimationFrame.js' export default { props: { startVal: { type: Number, required: false, default: 0 }, endVal: { type: Number, required: false, default: 2017 }, duration: { type: Number, required: false, default: 3000 }, autoplay: { type: Boolean, required: false, default: true }, decimals: { type: Number, required: false, default: 0, validator (value) { return value >= 0 } }, decimal: { type: String, required: false, default: '.' }, separator: { type: String, required: false, default: ',' }, prefix: { type: String, required: false, default: '' }, suffix: { type: String, required: false, default: '' }, useEasing: { type: Boolean, required: false, default: true }, easingFn: { type: Function, default (t, b, c, d) { return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b } } }, data () { return { localStartVal: this.startVal, displayValue: this.formatNumber(this.startVal), printVal: null, paused: false, localDuration: this.duration, startTime: null, timestamp: null, remaining: null, rAF: null } }, computed: { countDown () { return this.startVal > this.endVal } }, watch: { startVal () { if (this.autoplay) { this.start() } }, endVal () { if (this.autoplay) { this.start() } } }, mounted () { if (this.autoplay) { this.start() } this.$emit('mountedCallback') }, methods: { start () { this.localStartVal = this.startVal this.startTime = null this.localDuration = this.duration this.paused = false this.rAF = requestAnimationFrame(this.count) }, pauseResume () { if (this.paused) { this.resume() this.paused = false } else { this.pause() this.paused = true } }, pause () { cancelAnimationFrame(this.rAF) }, resume () { this.startTime = null this.localDuration = +this.remaining this.localStartVal = +this.printVal requestAnimationFrame(this.count) }, reset () { this.startTime = null cancelAnimationFrame(this.rAF) this.displayValue = this.formatNumber(this.startVal) }, count (timestamp) { if (!this.startTime) this.startTime = timestamp this.timestamp = timestamp const progress = timestamp - this.startTime this.remaining = this.localDuration - progress if (this.useEasing) { if (this.countDown) { this.printVal = this.localStartVal - this.easingFn(progress, 0, this.localStartVal - this.endVal, this.localDuration) } else { this.printVal = this.easingFn(progress, this.localStartVal, this.endVal - this.localStartVal, this.localDuration) } } else { if (this.countDown) { this.printVal = this.localStartVal - ((this.localStartVal - this.endVal) * (progress / this.localDuration)) } else { this.printVal = this.localStartVal + (this.endVal - this.localStartVal) * (progress / this.localDuration) } } if (this.countDown) { this.printVal = this.printVal < this.endVal ? this.endVal : this.printVal } else { this.printVal = this.printVal > this.endVal ? this.endVal : this.printVal } this.displayValue = this.formatNumber(this.printVal) if (progress < this.localDuration) { this.rAF = requestAnimationFrame(this.count) } else { this.$emit('callback') } }, isNumber (val) { return !isNaN(parseFloat(val)) }, formatNumber (num) { num = num.toFixed(this.decimals) num += '' const x = num.split('.') let x1 = x[0] const x2 = x.length > 1 ? this.decimal + x[1] : '' const rgx = /(\d+)(\d{3})/ if (this.separator && !this.isNumber(this.separator)) { while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + this.separator + '$2') } } return this.prefix + x1 + x2 + this.suffix } }, unmounted () { cancelAnimationFrame(this.rAF) } } </script>
到此這篇關(guān)于vue3使用vue-count-to組件的文章就介紹到這了,更多相關(guān)vue3 vue-count-to組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue.js的vue-cli腳手架中使用百度地圖API的實(shí)例
今天小編就為大家分享一篇關(guān)于vue.js的vue-cli腳手架中使用百度地圖API的實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01利用vue-i18n實(shí)現(xiàn)多語言切換效果的方法
這篇文章主要給大家介紹了關(guān)于利用vue-i18n實(shí)現(xiàn)多語言切換效果的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue-i18n具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Vue中關(guān)于computed計(jì)算屬性的妙用
這篇文章主要介紹了Vue中關(guān)于computed計(jì)算屬性的妙用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11解決vuejs 使用value in list 循環(huán)遍歷數(shù)組出現(xiàn)警告的問題
今天小編就為大家分享一篇解決vuejs 使用value in list 循環(huán)遍歷數(shù)組出現(xiàn)警告的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09Vue簡單封裝axios之解決post請(qǐng)求后端接收不到參數(shù)問題
這篇文章主要介紹了Vue簡單封裝axios之解決post請(qǐng)求后端接收不到參數(shù)問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02解決Element組件的坑:抽屜drawer和彈窗dialog
這篇文章主要介紹了解決Element組件的坑:抽屜drawer和彈窗dialog問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07Vue.js仿Metronic高級(jí)表格(二)數(shù)據(jù)渲染
這篇文章主要為大家詳細(xì)介紹了Vue.js仿Metronic高級(jí)表格的數(shù)據(jù)渲染,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04vue限制文本輸入框只允許輸入字母、數(shù)字、禁止輸入特殊字符
這篇文章主要介紹了vue限制文本輸入框只允許輸入字母、數(shù)字、不允許輸入特殊字符,通過監(jiān)聽表單輸入的內(nèi)容,使用方法的缺陷,本文通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友參考下吧2023-10-10在Vue3中創(chuàng)建和使用全局組件的實(shí)現(xiàn)方式
在前端開發(fā)中,Vue.js 是一個(gè)廣泛使用的框架,因其靈活性和強(qiáng)大的功能,得到許多開發(fā)者的喜愛,Vue 3 的發(fā)布為這一框架帶來了很多新的特性和改進(jìn),在本文中,我們將詳細(xì)討論如何在 Vue 3 中創(chuàng)建和使用全局組件,并通過示例代碼展示具體實(shí)現(xiàn)方式,需要的朋友可以參考下2024-07-07