先放答案:
> ```javascript
mounted() {
const m = this.$refs.score; // countTo
// 監(jiān)聽 displayValue / 分?jǐn)?shù)的數(shù)字滾動(dòng)變化
const that = this;
Object.defineProperty(m, 'displayValue', {
get: function () {
return m.displayValue;
},
set: function (v) {? // 數(shù)據(jù)更新后會(huì)返回更新后的值
that.scoreVal = v;
}
})
}
```
前段時(shí)間做了個(gè)包含進(jìn)度條的 H5 頁(yè)面,要求進(jìn)度條和進(jìn)度值有個(gè)過渡動(dòng)畫。用的 countTo 這個(gè)數(shù)字滾動(dòng)插件,在這里記錄一下我監(jiān)聽的一個(gè)思路:
由于 countTo 并沒有 API 能夠直接監(jiān)聽到它這個(gè)數(shù)字的變化所以我們只能夠手動(dòng)去監(jiān)聽,利用 defineProperty 來進(jìn)行數(shù)據(jù)的劫持。