我正在学习如何使用AngularFirebase
,而我正面临着标题问题。
以下是代码:
@Injectable()
export class AbstractService<T> {
constructor(private dbRef: AngularFireDatabase,
private afAuthRef: AngularFireAuth,
private dbFireStoreRef: AngularFirestore,
private storageRef: AngularFireStorage,
private message: MessageService) {
this.dbRealTime = dbRef;
this.afAuth = afAuthRef;
this.dbFireStore = dbFireStoreRef;
this.storage = storageRef;
}
<-- some other methodes -->
getAll(path: string): Observable<T[]> {
return this.dbRealTime.list(path).valueChanges();
}
}
另一部分是
export class BooksService extends AbstractService<Book> {
addBook(book: Book) {
return new Promise(
(resolve, reject) => {
this.dbRealTime.list(this.suffixes.books).push(book).then(
() => resolve(),
(error) => reject(error));
});
}
getAllBooks(): Observable<Book[]> {
return this.getAll(this.suffixes.books);
}
}
但我不明白为什么我会这样做。有这个错误:
ERROR in src/app/common/services/abstract.service.ts(59,5): error TS2322:
Type 'Observable<{}[]>' is not assignable to type 'Observable<T[]>'.
Type '{}[]' is not assignable to type 'T[]'.
Type '{}' is not assignable to type 'T'.
我已经在StackOverflow
上尝试了其他问题,例如在方法和括号之间放置<T[]>
来转换响应(或事件之前和之后),但没有成功。
感谢您的阅读,希望您能提供帮助。