无法连接到AngularFireDatabase

时间:2019-02-26 12:31:02

标签: angular typescript firebase firebase-realtime-database

我正在尝试构建一个Angular应用程序,该应用程序使用最新版本的AngularFire模块从Firebase实时NoSQL数据库中提取数据。我一直在关注https://github.com/angular/angularfire2/blob/master/docs/install-and-setup.mdhttps://github.com/angular/angularfire2/blob/master/docs/rtdb/lists.md列出的文档。但是,当我运行ng serve时,我只能看到一个空白页,并且控制台中的此错误重复了两次:NullInjectorError: No provider for AngularFireDatabase!

我已经阅读了所有文档,并且似乎更明确地连接到Firestore,而不是Firebase,这对我来说不那么容易理解。非常感谢您对我可能做错的事情的帮助或反馈。

这是我的 app.module.ts 文件:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { AngularFireModule } from '@angular/fire';
import { environment } from '../environments/environment';
import { AngularFirestoreModule } from '@angular/fire/firestore';
import { AngularFireStorageModule } from '@angular/fire/storage';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFirestoreModule,
    AngularFireStorageModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts 文件:

import { Component } from '@angular/core';
import { AngularFireDatabase } from '@angular/fire/database';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  study_items: Observable<any[]>;
  constructor(db: AngularFireDatabase) {
     this.study_items = db.list('study_items').valueChanges(); 
//      console.log(this);
  }
}

environment.ts 文件:

export const environment = {
  production: false,
  firebase: {
    apiKey: '<my api key because I am not showing it>',
    authDomain: 'my-firebase-demo.firebaseapp.com',
    databaseURL: 'https://my-firebase-demo.firebaseio.com',
    projectId: 'my-firebase-demo',
    storageBucket: 'my-firebase-demo.appspot.com',
    messagingSenderId: '<somenumber>'
  }
};

最后是 app.component.html 文件

<ul>
  <li class="text" *ngFor = "let unit of study_items | async">
    {{ unit | json }}
  </li>
</ul>

0 个答案:

没有答案