繁体   English   中英

谷歌登录后显示用户daya ionic

[英]Displaying user daya after google login ionic

制作这个应用程序几个小时后,我被谷歌登录卡住了,我之前问过问题但仍然卡住了。

所以我有 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');
      })
    })
  }

然后我尝试获取用户数据并将其显示在其他页面上,但我被卡住了,因为我没有得到我需要的东西。 在我的 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');
  }

我调用数据并将其解析为对象,当我调用 console.log(user) 时,它向我显示了这一点

在此处输入图片说明

但是当我尝试调用 user.displayName 它说 undifined

不明确的

更新我的代码后,我可以记录我的数据,但我正在尝试像这样在 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>

但现在我收到这样的错误

在此处输入图片说明

您正在访问不正确的对象,而是使用:

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

此外,如评论中所述,您需要将userData定义为类级别,以便它在您的模板中可见(确保您不要在构造函数中重新定义它,因为它会隐藏类级别变量)。

暂无
暂无

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

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