Javascript Vue用户对象为空,即使它已定义并显示在日志中

Javascript Vue用户对象为空,即使它已定义并显示在日志中,javascript,vue.js,axios,Javascript,Vue.js,Axios,我正在调用一个函数,该函数使用一个人的用户名在我的数据库中查找当前用户。当我在我的函数中记录当前用户时,它可以正常工作,但在这之后,每当我尝试访问它时,它都会说它为null或未定义 在我的发言中: mounted() { this.getCurrentUser(this.currentUserName); console.log(this.currentUser) } 在我的getCurrentUser()中: 在my UserDataService中: getCurre

我正在调用一个函数,该函数使用一个人的用户名在我的数据库中查找当前用户。当我在我的函数中记录当前用户时,它可以正常工作,但在这之后,每当我尝试访问它时,它都会说它为null或未定义

在我的发言中:

mounted() {
    this.getCurrentUser(this.currentUserName);
    console.log(this.currentUser)

  }
在我的getCurrentUser()中:

在my UserDataService中:

getCurrentUser(userName){
        return http.get("/users?userName="+userName);
     }


否则,在我将其登录到mounted()后,它将为null。

您进行异步调用。
getCurrentUser
的结果现在不可用

方法中的日志会等待,但在
mounted
中,您不会等待

您可以使
mounted
异步并等待结果,但同时会有一个
空的
组件

async mounted() {
    await this.getCurrentUser(this.currentUserName);
    console.log(this.currentUser)
}
并从
getCurrentUser

getCurrentUser(currentUserName){
   return UserDataService.getCurrentUser(currentUserName)

您可以进行异步调用。
getCurrentUser
的结果现在不可用

方法中的日志会等待,但在
mounted
中,您不会等待

您可以使
mounted
异步并等待结果,但同时会有一个
空的
组件

async mounted() {
    await this.getCurrentUser(this.currentUserName);
    console.log(this.currentUser)
}
并从
getCurrentUser

getCurrentUser(currentUserName){
   return UserDataService.getCurrentUser(currentUserName)

这也回答了你的问题吗?这也回答了你的问题吗?