最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3使用vue-count-to組件的實(shí)現(xiàn)

 更新時(shí)間:2020年12月25日 08:44:51   作者:TwoKe  
這篇文章主要介紹了vue3使用vue-count-to組件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

項(xiàng)目場景:

數(shù)據(jù)可視化大屏開發(fā)的過程中,需要實(shí)現(xiàn)一種滾動數(shù)字的效果,在使用vue2時(shí),使用vue-count-to完全沒有問題,功能也比較完善(滾動時(shí)長,開始值,結(jié)束值,前綴,后綴,千分隔符,小數(shù)分隔符等等),但是在vue3中使用會出現(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,則會退到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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue背景圖片路徑問題及解決

    vue背景圖片路徑問題及解決

    這篇文章主要介紹了vue背景圖片路徑問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • vue2使用?element表格展開功能渲染子表格的方式

    vue2使用?element表格展開功能渲染子表格的方式

    這篇文章主要介紹了vue2使用?element表格展開功能渲染子表格的方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-01-01
  • vue 使用vant插件做tabs切換和無限加載功能的實(shí)現(xiàn)

    vue 使用vant插件做tabs切換和無限加載功能的實(shí)現(xiàn)

    這篇文章主要介紹了vue 使用vant插件做tabs切換和無限加載功能的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • vue單頁面應(yīng)用部署配置詳解

    vue單頁面應(yīng)用部署配置詳解

    本文主要介紹了vue單頁面應(yīng)用部署配置詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • vue自動化表單實(shí)例分析

    vue自動化表單實(shí)例分析

    本篇文章通過實(shí)例給大家分享了vue自動化表單的操作方法以及相關(guān)的代碼做了描述,有興趣的朋友可以跟著學(xué)習(xí)下。
    2018-05-05
  • vue實(shí)現(xiàn)把頁面導(dǎo)出成word文件的方法

    vue實(shí)現(xiàn)把頁面導(dǎo)出成word文件的方法

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)把頁面導(dǎo)出成word文件的方法,文中的實(shí)現(xiàn)步驟講解詳細(xì),并且有詳細(xì)的代碼示例,需要的小伙伴可以參考一下
    2023-10-10
  • vue中vite.config.js配置跨域以及環(huán)境配置方式

    vue中vite.config.js配置跨域以及環(huán)境配置方式

    這篇文章主要介紹了vue中vite.config.js配置跨域以及環(huán)境配置方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Vue3.0版本強(qiáng)勢升級點(diǎn)特性詳解

    Vue3.0版本強(qiáng)勢升級點(diǎn)特性詳解

    這篇文章主要介紹了Vue3.0版本強(qiáng)勢升級點(diǎn)特性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>
    2022-06-06
  • vue+elementUi中的table實(shí)現(xiàn)跨頁多選功能(示例詳解)

    vue+elementUi中的table實(shí)現(xiàn)跨頁多選功能(示例詳解)

    最近在開發(fā)工業(yè)品超市的后臺系統(tǒng),遇到一個(gè)需求,就是實(shí)現(xiàn)在一個(gè)table表格中多選數(shù)據(jù),在網(wǎng)上查了好多,有些方法真的是無語,下面通過本文給大家分享vue+elementUi中的table實(shí)現(xiàn)跨頁多選功能,感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • Vue實(shí)現(xiàn)導(dǎo)入Excel功能步驟詳解

    Vue實(shí)現(xiàn)導(dǎo)入Excel功能步驟詳解

    這篇文章主要介紹了Vue實(shí)現(xiàn)導(dǎo)入Excel功能,本文分步驟給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-07-07

最新評論

五大连池市| 南漳县| 鸡西市| 安阳县| 陇川县| 昭通市| 安顺市| 胶州市| 临城县| 玉溪市| 本溪市| 尼玛县| 石首市| 灵武市| 怀宁县| 雷州市| 尚志市| 龙山县| 盐池县| 永善县| 枣阳市| 上犹县| 贵溪市| 鄂尔多斯市| 建平县| 商水县| 莱芜市| 临西县| 子洲县| 全州县| 云阳县| 大悟县| 新蔡县| 滦平县| 乌什县| 广元市| 镇宁| 清涧县| 合肥市| 通州市| 红桥区|