Does Vue.JS work with AJAX http calls?

前端 未结 3 600
情书的邮戳
情书的邮戳 2020-11-30 16:12

I am trying to do the following from my HTML:

var vm = new Vue({
      el: \'#loginContent\',
      data: {
        main_message: \'Login\',
        isLogged         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 16:13

    I would propose another method use ES6 Arrow Functions like '=>'. It is simple and do not need extra variable.Like following:

          $.ajax({
            url: '/api/login',
            data: data,
            method: 'POST'
          }).then((response) => {
            if(response.error) {
              console.err("There was an error " + response.error);
              this.loginError = 'Error';
            } else {
              //$('#loginBlock').attr("hidden",true);
              console.log(response.user);
              if(response.user) {
                this.isLoggedIn = true;
              } else {
                this.loginError = 'User not found';
              }
            }
          }).catch(function (err) {
            console.error(err);
          });
    

提交回复
热议问题