简体   繁体   English

谷歌登录后显示用户daya ionic

[英]Displaying user daya after google login ionic

After couple hours making this app, i'm stuck with google login, i've ask question before but still stuck.制作这个应用程序几个小时后,我被谷歌登录卡住了,我之前问过问题但仍然卡住了。

so i have logic.service.ts which handle user to login and get their data store them in localStorage所以我有 logic.service.ts 处理用户登录并将他们的数据存储在 localStorage 中

login(){
    this.googlePlus.login({
      'webClientId' : '927898787134-spvfdmvm9apq0e1fo2efvvura8vqpid8.apps.googleusercontent.com',
      'offile' : true
    }).then(res=>{
      firebase.auth().signInWithCredential(firebase.auth.GoogleAuthProvider.credential(res.idToken))
      .then(suc=>{
        // console.log('users', JSON.stringify(suc));
        this.storage.set('user', JSON.stringify(suc));
        this.router.navigate(["/tabs/home"]);
      }).catch(ns=>{
        alert('Unsucessful');
      })
    })
  }

and then i try to get user data and display it on other page, but i'm stuck because i don't get things that i needed.然后我尝试获取用户数据并将其显示在其他页面上,但我被卡住了,因为我没有得到我需要的东西。 in my home.page.ts在我的 home.page.ts

     userData:any;
  loginDetails:any;
  subscribe: any;
  user:any={};
  public items: any;
  constructor(
    private iab: InAppBrowser,
    private router: Router,
    public platform: Platform,
    private service: LogicService,
    private nativeStorage: NativeStorage
    ) {

    this.subscribe = this.platform.backButton.subscribeWithPriority(666666, () => {
      if (this.constructor.name === 'HomePage') {
        if (window.confirm('Do you want to exit the app?')) {
          (navigator as any).app.exitApp();
        }
      }
    });
    let userData = JSON.parse(localStorage.getItem('user'));
    console.log(userData.user);
    console.log(userData.user.displayName);
    console.log(userData.user.email);
    console.log(userData.user.photoURL);
  }
  go() {
      this.router.navigateByUrl('tabs/login');
  }

  ngOnInit(){
    this.service.getLocalData().subscribe(
      data => {this.items = data});
  }
  webview(url){
    this.iab.create(url, '_self', 'location=no,toolbar=no,zoom=no');
  }

i call the data and parse it into object, when i call console.log(user) it shows me this我调用数据并将其解析为对象,当我调用 console.log(user) 时,它向我显示了这一点

在此处输入图片说明

but when i try to call user.displayName it says undifined但是当我尝试调用 user.displayName 它说 undifined

不明确的

after updated my code, i'm able to log my data but i'm trying to display it on html like this更新我的代码后,我可以记录我的数据,但我正在尝试像这样在 html 上显示它

<div class="header">
          <h3 style="font-weight: bold;">{{ userData.user.displayName }}</h3>
          <p style="color:grey;font-weight:bold;">Get closer with us!</p>
        </div>

but now i get an error like this但现在我收到这样的错误

在此处输入图片说明

You are accessing the incorrect object, instead use:您正在访问不正确的对象,而是使用:

let userData = JSON.parse(localStorage.getItem('user'));
console.log(userData.user.displayName);

Also, as mentioned in the comments you need to define userData as a class level so that it is visible in your template (make sure you don't redefine it in the constructor as it will then hide the class-level variable).此外,如评论中所述,您需要将userData定义为类级别,以便它在您的模板中可见(确保您不要在构造函数中重新定义它,因为它会隐藏类级别变量)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM