Vue + Axios发布请求响应数据返回未定义

时间:2019-01-26 13:19:21

标签: vue.js axios adonis.js

在我的vue js项目中,我有一个services文件夹,用于处理与服务器的交互。在我的服务文件夹中,我有一个处理身份验证的类 Auth.js

export default class Auth {
  login(email,password {
    let user = {email: email, password: password}
    axios.post("/login", user)
      .then(res => {
        return res.data
      })
  }
}

App.vue

  import Auth from './services/Auth'
  let auth = new Auth();
  export default {
    data: () => ({
      drawer: null,
      email: '',
      password: ''
    }),
    methods: {
      login() {        
        let user = auth.login(this.email, this.password)
        console.log(user) // returns undefined
      }
    }
  }

由于某种原因,用户未定义,但是当我在chrome中检查“网络”标签时,当我向/api/register发送对我的Adonis应用程序的发布请求时,可以看到返回的用户对象。请帮忙。

1 个答案:

答案 0 :(得分:0)

固定

Auth.js

export default class Auth {
    login(email,password {
        let user = {email: email, password: password}
        return axios.post("/login", user)
    }
}

App.vue

import Auth from './services/Auth'
let auth = new Auth();
export default {
    data: () => ({
        drawer: null,
        email: '',
        password: ''
    }),
    methods: {
        login() {        
            auth.login(this.email, this.password)
                .then(res => console.log(res.data))
        }
    }
}