I am trying to do the following from my HTML:
var vm = new Vue({
el: '#loginContent',
data: {
main_message: 'Login',
isLoggedIn: false,
loginError: '',
loginButton:'Login'
},
methods: {
onLogin: function() {
var data = {
email: $('#email').val(),
password: $('#password').val(),
};
$.ajax({
url: '/api/login',
data: data,
method: 'POST'
}).then(function (response) {
if(response.error) {
console.err("There was an error " + response.error);
this.loginError = 'Error';
} else {
console.log(response.user);
if(response.user) {
this.isLoggedIn = true;
} else {
this.loginError = 'User not found';
}
}
}).catch(function (err) {
console.error(err);
});
}
}
});
Basically, the user presses the login button, the onLogin method is called, which sends a message to my API. The message is working fine and I will return the answer in the promise .then ().
But trying to do things like this.isLoggedIn = true;, does not update my DOM with what I expect from HTML when a user logs in.
Could it be that I'm in some kind of background thread (sorry, mobile developer is here) when I get the answer in the promise and he cannot find the instance of "vm"?
thank
source
share