[Vuejs]-Update a data value from a method called in mounted

3👍

That’s because of the callback you’re passing into your scroll listener. In your callback this is not the context of the vue component. I think its the body actually. You’ll need to use an arrow function or pass the vue instance into the callback as an argument. Arrow function is easiest. An arrow function retains the context where it was defined rather than inheriting the context of the object that calls it. Here is a good post/answer about the difference between normal and arrow functions.

document.querySelector('body').onscroll = () => {
    console.log(window.pageYOffset > 0)
    this.sticky = window.pageYOffset > 0
}

Leave a comment