Javascript 如何构建Vue Axios服务以返回承诺树

Javascript 如何构建Vue Axios服务以返回承诺树,javascript,vue.js,axios,Javascript,Vue.js,Axios,我知道我让事情变得更难了 vue方法 methods: { userLogin: function() { var loginJson = [] loginJson = JSON.stringify(this.login); UserService.login(loginJson); //Somehow read the promise, then and catch. // like

我知道我让事情变得更难了

vue方法

methods: {
    userLogin: function()
    {
        var loginJson = []
        loginJson = JSON.stringify(this.login);
        UserService.login(loginJson);
       //Somehow read the promise, then and catch. 
       // like 
       // loginReturn.then(function(response){
       //     console.log(response);
       // })
        //.catch(function(error){
        //    console.log(error);
        //})                 
    }
}
正在尝试在js文件中为vue创建一些服务

var axios = require('axios')
export default { //I don't think this is correct?

    //Set up some build variable    
    login(data){
        let baseUrl = "http://coolwebsite.com/api/user/login";
        return axios.post(baseUrl, data);

          // .then(function (response) {
          //   console.log(response);
          // })
          // .catch(function (error) {
          //   console.log(error);
          // });

    }
}

我想把承诺还给函数。让登录函数处理承诺,并在屏幕上找出需要发生的事情

根据您发布的内容,应该是:

UserService.login(loginJson).then(response => {
   // handle response...
}).catch(error => {
   // handle error...
})

您已经设置了
登录
以从axios返回承诺,因此只需继续链。

根据您发布的内容,它将是:

UserService.login(loginJson).then(response => {
   // handle response...
}).catch(error => {
   // handle error...
})

您已经设置了
login
以返回axios的承诺,所以请继续链。

我不明白。什么不起作用?您已经从第二个文件的
login()
向第一个文件的
userLogin()
返回了承诺。你应该在第一个文件中
。然后()
。我不明白。什么不起作用?您已经从第二个文件的
login()
向第一个文件的
userLogin()
返回了承诺。您应该在第一个文件中使用
.then()