加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

身份验证 – UID不显示构造函数

发布时间:2020-12-17 07:15:16 所属栏目:安全 来源:网络整理
导读:我试图让uid脱离构造函数时遇到一些问题.当我运行此代码时,uid在Test 1日志中完美显示.但是Test2日志显示未定义.当构造函数关闭时,uid会删除自己吗? import { Component,OnInit } from '@angular/core';import {AngularFire,FirebaSEObjectObservable,Fireb
我试图让uid脱离构造函数时遇到一些问题.当我运行此代码时,uid在Test 1日志中完美显示.但是Test2日志显示未定义.当构造函数关闭时,uid会删除自己吗?

import { Component,OnInit } from '@angular/core';
import {AngularFire,FirebaSEObjectObservable,FirebaseListObservable} from 'angularfire2';

@Component({
  selector: 'dashboard',templateUrl: 'dashboard.component.html',styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent {
  userid:string;
  users:FirebaseListObservable<any[]>;
  constructor(public af: AngularFire) {
    this.af.auth.subscribe(auth => {
      console.log(auth.uid);
      this.userid = auth.uid;
      console.log("Test 1 "+ this.userid);
    });
    console.log("Test2 "+ this.userid);
  }
}

这是我在控制台中的输出

Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
dashboard.component.ts:18 Test2 undefined
dashboard.component.ts:14 CgTltJ1h2TUFS5teoACCxoPHP1g1
dashboard.component.ts:16 Test 1 CgTltJ1h2TUFS5teoACCxoPHP1g1

我的第二个问题是如何从构造函数中获取uid?因为我需要它从数据库中获取一些其他数据.

解决方法

执行console.log(“Test2 …”)时,该值尚不可用

当数据从服务器到达时,函数传递给subscribe()

auth => {
      console.log(auth.uid);
      this.userid = auth.uid;
      console.log("Test 1 "+ this.userid);
    }

被调用,只有这时数据才可用,只有在该函数内,console.log(“Test2 …”)在此之前执行了一段时间

export class DashboardComponent {
  userid:string;
  users:FirebaseListObservable<any[]>;
  constructor(public af: AngularFire) {
    this.af.auth.subscribe(auth => {
      console.log(auth.uid);
      this.userid = auth.uid;
      console.log("Test 1 "+ this.userid);
    });
    console.log("Test2 "+ this.userid);
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读