无法将对象从方法传递到数据变量?

时间:2019-03-13 15:36:01

标签: vue.js

已安装-> 呼叫 GoogleSignIn() 方法,该方法会从Google帐户获取var user,然后我使对象从var user传递到名为localUser的数据变量。但是,什么也没有发生,当我尝试在<span>{{ localUser.displayName }}</span>中显示该错误消息时,我什至没有收到错误消息。

我尝试使用的是this.$data.localUser = user,它是Vue 0x引用,但没有用。 我也尝试将localUser类型设置为ObjectString。 我已经玩了更多的游戏,但是一个多小时半后无法使它正常工作。

到目前为止,仅对我有用的是在设置console.log("Localuser:" + this.localUser.displayName)之后(同时在方法中)设置控制台日志this.localUser = user;

<template>
  <div class="home">
    <img class="v" alt="Vue logo" src="../assets/cbsc3-n4s6k.svg">
    <span>{{ localUser.displayName }}</span>
  </div>
</template>

<script>

import router from '../router'

export default {
  name: 'Login',
  components: {
    
  },
  data: function() {
    return {
      localUser: {}
    }
  },
  methods: {
    GoogleSignIn: function (event) {
      var provider = new firebase.auth.GoogleAuthProvider();
      firebase.auth().languageCode = 'pt';


      firebase.auth().onAuthStateChanged(function(user) {
        if (user) {
          // If already signed in.
          console.log(user.displayName);
          this.localUser = user;
          // This console.log actually works and logs the Google account name
          console.log("Localuser:" + this.localUser.displayName)
          // router.push("Home");
        } else {
          // If no one is signed in.
          firebase.auth().signInWithPopup(provider).then(function(result) {

                console.log("Logged in.");
                var token = result.credential.accessToken;
                var user = result.user;
                this.localUser = user;
                console.log("Localuser:" + this.localUser.displayName)

          }).catch(function(error) {
                var errorCode = error.code;
                var errorMessage = error.message;
                var email = error.email;
                // The firebase.auth.AuthCredential type that was used.
                var credential = error.credential;
                console.log(errorMessage);
          });

        }
      });
    }
  },
  mounted() {
    console.log("Mounted");
    this.GoogleSignIn();
  }
}
</script>

0 个答案:

没有答案