我不明白發生了什么,它應該很簡單,但由于某種原因,我得到undefined了一個屬性,當我除錯它時,我可以看到它在那里,不是未定義的。組件已安裝,功能正在安裝中使用,一切都應該在那里并且可以正常作業。但是 Vue 一直說它是undefined.
這是模板:
<template>
<div class="AnimatedCounter">
<span ref="counterText" class="AnimatedCounter-text">{{ count }}</span>
</div>
</template>
mounted() {
this.setCount(this.counter);
},
methods: {
setCount: (val) => {
/* $refs props and $el is "undefined" at runtime */
const obj = this.$refs.counterText;
anime({
targets: this.$el,
n: val,
round: 1,
duration: 500,
easing: "linear",
update: () => {
this.count = obj.n;
}
});
}
}
無論我做什么,我都會繼續做同樣的事情。我錯過了什么?
uj5u.com熱心網友回復:
問題出在您撰寫函式的方式上。
您使用了箭頭函式,它不會將this關鍵字系結到 Vue 組件。它很可能參考了Window
您需要使用function關鍵字來宣告您的函式,如下所示:
mounted() {
this.setCount(this.counter);
},
methods: {
setCount: function (val) {
const obj = this.$refs.counterText;
anime({
targets: this.$el,
n: val,
round: 1,
duration: 500,
easing: "linear",
update: () => {
this.count = obj.n;
}
});
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/388471.html
標籤:javascript Vue.js
上一篇:如何在vuejs中動態更新細節?
下一篇:Vuejs的性能指標
