在模块中添加AngularFirestoreModule和AngularFirestoreCollection的引用。
示例:
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { AngularFirestoreModule } from '@angular/fire/firestore'; import { AngularFirestoreCollection } from '@angular/fire/firestore';
@NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AngularFirestoreModule ], providers: [ AngularFirestoreCollection ], bootstrap: [AppComponent] }) export class AppModule { }
import { Component } from '@angular/core'; import { AngularFirestoreCollection } from '@angular/fire/firestore'; import { Observable } from 'rxjs'; import { Item } from './item.model'; import { AngularFirestore } from '@angular/fire/firestore';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
itemsCollection: AngularFirestoreCollection
constructor(private afs: AngularFirestore) { this.itemsCollection = this.afs.collection('items'); this.items = this.itemsCollection.valueChanges(); } }
通过以上示例,你就可以在Angular 13环境下成功使用AngularFirestoreCollection了。