在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ vue里countup一直報(bào)錯(cuò)是什么原因?

vue里countup一直報(bào)錯(cuò)是什么原因?

上代碼:

<template>
    <span :class="commonClass" :style="commonStyle">
        <span :class="selfClass" :style="selfStyle" :id="countId">{{ initCount }}</span>
    </span>
</template>

<script>
import CountUp from 'countup'
export default {
    data () {
        return {
            count: {},
            countId: 'count' + parseInt(Math.random() * 1000000)
        }
    },
    props: {
        // 公用的class
        commonClass: {
            type: String,
            default: ''
        },
        // 公用的style
        commonStyle: {
            type: Object,
            default: () => {
                return {
                    fontSize: '30px',
                    color: '#dc9387'
                }
            }
        },
        // 私有的class
        selfClass: {
            type: String,
            default: ''
        },
        // 私有的style
        selfStyle: {
            type: Object
        },
        // 初始的默認(rèn)值
        initCount: {
            type: Number,
            default: 0
        },
        // 舊值
        oldValue: {
            type: Number,
            default: 0
        },
        // 新值
        newValue: {
            type: Number,
            default: 0
        },
        // 保留小數(shù)位數(shù)
        decimals: {
            type: Number,
            default: 0
        },
        // 完整動(dòng)畫過渡的時(shí)間,單位秒
        duration: {
            type: Number,
            default: 2
        },
        // 是否線性過渡變化,false為是
        useEasing: {
            type: Boolean,
            default: false
        },
        // 是否每三位數(shù)字用符號(hào)標(biāo)識(shí)
        useGrouping: {
            type: Boolean,
            default: true
        },
        // 標(biāo)識(shí)的符號(hào)
        separator: {
            type: String,
            default: ','
        },
        // 小數(shù)的符號(hào)
        decimal: {
            type: String,
            default: ','
        },
        // 前置展示的文字
        prefix: {
            type: String,
            default: ''
        },
        // 后置展示的文字
        suffix: {
            type: String,
            default: ''
        },
        // 延遲開始過渡動(dòng)畫時(shí)間,單位毫秒
        delay: {
            type: Number,
            default: 0
        }
    },
    methods: {
    },
    mounted () {
        this.$nextTick(() => {
            setTimeout(() => {
                this.count = new CountUp(this.countId, this.oldValue, this.newValue, this.decimals, this.duration, {
                    useEasing: this.useEasing,
                    useGrouping: this.useGrouping,
                    separator: this.separator,
                    decimal: this.decimal,
                    prefix: this.prefix,
                    suffix: this.suffix
                });
                if (!this.count.error) {
                    this.count.start();
                } else {
                ??console.error(this.count.error);
                }
            }, this.delay)
        })
    },
    watch: {
        'newValue' (val) {
            this.count.update(val)
        }
    }
}
</script>

打開頁面數(shù)據(jù)展示倒是沒什么問題,但是一直報(bào)錯(cuò)Error in callback for watcher "newValue": "TypeError: this.count.update is not a function"
這是為什么?
另:現(xiàn)在要做的效果是一個(gè)接口返回所有數(shù)據(jù)復(fù)用幾次這個(gè)組件,然后有各自的延遲,但是我這么寫實(shí)際測(cè)了下只有第一次是有延遲的,等到第二次調(diào)接口返回?cái)?shù)據(jù),所有歌計(jì)數(shù)都同時(shí)開始改變了,這該怎么寫?

回答
編輯回答
選擇

@travins @MiIIer 首先謝謝兩位伙伴的指點(diǎn)!既然問題已經(jīng)解決了,我就說說與問題無關(guān)的、也是初學(xué)者經(jīng)常出錯(cuò)的話題。

關(guān)于vue.js中箭頭函數(shù)this的指向

所有的生命周期鉤子自動(dòng)綁定 this 上下文到實(shí)例中,因此你可以訪問數(shù)據(jù),對(duì)屬性和方法進(jìn)行運(yùn)算。這意味著 你不能使用箭頭函數(shù)來定義一個(gè)生命周期方法 (例如 created: () => this.fetchTodos())。這是因?yàn)榧^函數(shù)綁定了父上下文,因此 this 與你期待的 Vue 實(shí)例不同, this.fetchTodos 的行為未定義。

而生命周期鉤子內(nèi)部的箭頭函數(shù)相當(dāng)于匿名函數(shù),并且簡化了函數(shù)定義??瓷先ナ悄涿瘮?shù)的一種簡寫,但實(shí)際上,箭頭函數(shù)和匿名函數(shù)有個(gè)明顯的區(qū)別:箭頭函數(shù)內(nèi)部的this是詞法作用域,由上下文確定。此時(shí)this在箭頭函數(shù)中已經(jīng)按照詞法作用域綁定了。很明顯,使用箭頭函數(shù)之后,箭頭函數(shù)指向的函數(shù)內(nèi)部的this已經(jīng)綁定了外部的vue實(shí)例了。

2017年9月23日 03:03
編輯回答
荒城

你的count 初始化的時(shí)候不是 {}嘛,哪里調(diào)的update?

2017年5月28日 10:28
編輯回答
尐飯團(tuán)

寫的很清楚啊,錯(cuò)誤是this.count.update不是個(gè)函數(shù),你的newValue為啥要加個(gè)引號(hào)?

2018年5月16日 07:15
編輯回答
單眼皮

第一點(diǎn)是'newValue'引號(hào)問題;
第二點(diǎn)是watch那個(gè)this.count執(zhí)行次序在setTimeout之后,所以首次執(zhí)行并沒有new CountUp成功,解決方法是要么忽略報(bào)錯(cuò)并不影響使用,要么在watch那里直接再new一次CountUp取得里面的update方法,這個(gè)應(yīng)該是iview admin里面封裝的一個(gè)組建,用它的腳手架并不會(huì)報(bào)錯(cuò),這點(diǎn)還得請(qǐng)iview作者Aresn大神來分析了。

之前的方法有點(diǎn)問題,還有一個(gè)辦法是在watch那里寫個(gè)延遲

setTimeout(()=>{
    this.count.update(endVal);
},this.delay+10)
2017年7月2日 09:47