You could try to make it like this
const app = new Vue({
el: '#app',
data: {
scrollPosition: null
},
methods: {
updateScroll() {
this.scrollPosition = window.scrollY
}
},
mounted() {
window.addEventListener('scroll', this.updateScroll);
}
})
You should also consider removing event listener when component is being destroyed, in order to prevent leaks:
destroy() {
window.removeEventListener('scroll', this.updateScroll)
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…