2017-10-20 62 views
1

我使用角4火力点,我需要等待装入我的服务,然后从我的数据库中创建FirebaseListObservable .. 我component.ts代码如下角4火力点等待服务被加载第一

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

的问题是,构造得到authService.cusername作为undendifed因为authService尚未完全加载..有什么办法可以等待服务被加载,然后再运行db.list('challenges', { etc...});?我试图做一些对ngOnInit但似乎我可以使用DB:仅在构造函数中AngularFireDatabase ..

回答

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; 
}