I am trying to detect the scroll event within the mounted () of my component and update the component's data accordingly.
・component
<script>
import trackScroll from '~/utils/trackScroll'
export default {
...
data() {
return {
scrollPosition: 0
}
},
mounted() {
window.addEventListener(
'scroll',
trackScroll(this.scrollPosition, window.scrollY)
)
},
</script>
・utils/trackScroll.js
export default function trackScroll(scrollPosition, scrollY) {
scrollPosition = scrollY
}
There are a couple of issues in this scenario:
1. The function is only triggered once during the initial scroll, but I want it to be executed every time there is a scroll event.
2. The value of this.scrollPosition
within the component remains unchanged despite the scrolling.
How can these problems be resolved?