[Vuejs]-VUE.JS/HTTP. The problem with getting a result of http query to an html using vue.js

0πŸ‘

βœ…

I suggest using the created hook instead of updated.

The bigger problem is the this context inside xhr.onreadystatechange. It won’t be pointing at the Vue instance. Using an arrow function is the simplest fix:

xhr.onreadystatechange = () => {

Arrow functions retain the this value from the surrounding scope.

The usual alternatives also apply, such as using bind on the function or grabbing this in a closure using const that = this. Vue automatically binds functions in the methods to the correct this value so if you introduced another method as the handler for onreadystatechange that would also work.

Leave a comment