数据可视化大屏开发的过程中,需要实现一种滚动数字的效果,在使用vue2时,使用vue-count-to完全没有问题,功能也比较完善(滚动时长,开始值,结束值,前缀,后缀,千分隔符,小数分隔符等等),但是在vue3中使用会出现问题。
Home |about
展示的效果
出现的错误时 == Cannot read property ‘_c' of undefined== 这是一个_c的属性没有找到,具体的情况也不是很清楚。在vue-count-to打包后的源码中可以大致看出来,这是在render函数中出现的错误。但是还是没法下手。
采用的方法是直接复制node_modules下vue-count-to的源文件(src下),到自己项目的components下。如图
然后根据eslint的检查,修改代码,直到不报错,且记删除package.json下刚刚引入的vue-count-to的依赖。如图
最后重启项目。
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在当前浏览器的实现形式
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']
}
// 如果当前浏览器不支持requestAnimationframe和cancelAnimationframe,则会退到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 }
{{displayValue}}
到此这篇关于vue3使用vue-count-to组件的文章就介绍到这了,更多相关vue3 vue-count-to组件内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!



