【问题标题】:Angular 4 firebase wait service to be loaded first首先加载Angular 4 firebase等待服务
【发布时间】:2018-04-01 17:44:21
【问题描述】:

我正在使用带有 firebase 的 Angular 4,我需要等待我的服务被加载,然后从我的数据库中创建 FirebaseListObservable.. 我的 component.ts 代码如下

constructor(db: AngularFireDatabase,public authService: AuthService,public 
    afAuth: AngularFireAuth) {  
        this.challenges = db.list('challenges', {
          query: {
            orderByChild: 'challenged',
            equalTo: this.authService.cusername
          }
        });

问题是构造函数将 authService.cusername 设为未定义,因为 authService 尚未完全加载。有什么方法可以等待服务先加载然后运行 ​​@ 987654322@??我试图在 ngOnInit 上做一些事情,但似乎我只能在构造函数中使用 db: AngularFireDatabase ..

【问题讨论】:

    标签: angular typescript firebase firebase-realtime-database angularfire


    【解决方案1】:

    只需在db: 之前添加privatepublic 关键字,这将使参数也成为类字段,因此您可以通过this.db 在其他方法中访问它:

    constructor(private db: AngularFireDatabase,public authService: AuthService,public 
    afAuth: AngularFireAuth) { }
    
    ngOnInit() {
        this.db.doSomething();
    }
    

    这基本上只是打字稿中的语法糖:

    constructor(private service: MyServiceType) {}
    

    等同于:

    private service: MyServiceType;
    
    constructor(service: MyServiceType) {
        this.service = service;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-06
      • 2018-04-04
      • 2022-11-30
      • 2018-05-07
      • 2019-01-18
      • 2018-11-16
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多