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.
Source:stackexchange.com