类型'AngularFirestore'不是通用的

时间:2018-01-21 00:20:42

标签: angular typescript firebase

我收到了错误

  

类型'AngularFirestore'不是通用的。

import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFirestore } from 'angularfire2/firestore';
import { Profile } from './../../models/profile';

export class LoginPage {

  profileCollection: AngularFirestore<Profile>

}

3 个答案:

答案 0 :(得分:1)

问题在于您将类型传递给AngularFireStore,但它不是通用的。尝试执行profileCollection: AngularFirestore而不是profileCollection: AngularFirestore<Profile>。在此处阅读有关泛型类型的更多信息:https://www.typescriptlang.org/docs/handbook/generics.html

答案 1 :(得分:1)

我认为您想要的个人资料集合的类型声明是profileCollection: Observable<Profile[]>;。只需确保您要导入&#39; rxjs / Observable&#39;。

Angular Firestore集合作为请求中的observable返回。

AngularFirestore实际上是一种用于调用数据库但不返回数据的服务。

答案 2 :(得分:0)

从Firestore获取此内容时,您将要使用不同的类型。首先,您希望LoginPage import AngularFirestore的构造函数在您的类顶部使用构造函数方法:

import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFirestore } from 'angularfire2/firestore';
import { Profile } from './../../models/profile';
import { Observable } from 'rxjs/Observable';

export class LoginPage {

  profileCollection: Observable<Profile[]>;

  constructor(
   private firestore: AngularFirestore
  ) {
   this.profileCollection = this.getProfiles();
  }

  getProfiles() {
   return this.firestore.collection(`collectionPath`).valueChanges();
  }

}

这是您在获取数据时应该注意的方向,但我对上面的代码感到有些困惑。这一切都包含在班级模型中吗?您应该在服务中制作所有Firestore请求,而不是在模型中处理此问题。该服务将使该代码易于在您的应用程序中重用。如果您需要更多帮助,请与我们联系。