我的组件看起来像这样:
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { AngularFirestore } from 'angularfire2/firestore';
@Component({
selector: 'app-mission',
templateUrl: './mission.component.html',
styleUrls: ['./mission.component.css']
})
export class MissionComponent {
public items: Observable<any[]>;
constructor(db: AngularFirestore) {
this.items = db.collection('/todos').valueChanges();
}
logForm(value: any) {
console.log(value);
this.db.collection("/todos").add({
name: "Los Angeles",
state: "CA",
country: "USA"
});
}
}
但是我不明白为什么this.db
有问题,并且每次遇到此错误时,Property 'db' does not exist on type 'MissionComponent'
可能我在这里丢失了一些东西。
答案 0 :(得分:1)
db仅在构造函数范围内有效。
export class MissionComponent {
public items: Observable<any[]>;
private db: AngularFirestore; // this will be this.db
constructor(db: AngularFirestore) {
this.items = db.collection('/todos').valueChanges();
this.db = db; // now you can use this.db
}