我一直在遵循一些关于从 angularfire2 v4 到 v5 的更新过程的指南,但无济于事。我似乎无法在需要的地方获取数据。我可以从 firebase 获取数据并将其显示在控制台中,但由于某种原因,我无法让它显示在我的 ngFor 循环中。有一种方法可以让它显示出来,但我也收到一个错误,所以我确定它是错误的。
我也试过完全按照这里的方式设置我的代码,什么也没有。https://alligator.io/angular/cloud-firestore-angularfire/
我得到的错误是
错误 TS2322:类型“{}[]”不可分配给类型“Chat[]”
继续我的代码。
聊天服务.ts
import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { Chat } from './../models/chat';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/take';
@Injectable()
export class ChatService {
constructor(
private db: AngularFireDatabase
) { }
getAll() {
return this.db.list('/chats');
}
聊天列表.component.ts
import { Component, OnInit } from '@angular/core';
import { AuthService } from './../../../admin/services/auth.service';
import { Observable } from 'rxjs/Observable';
import { AppUser } from './../../models/app-user';
import { ChatService } from './../../services/chat.service';
import { Chat } from './../../models/chat';
import * as firebase from 'firebase';
@Component({
selector: 'chat-list',
templateUrl: './chat-list.component.html',
styleUrls: ['./chat-list.component.css']
})
export class ChatListComponent implements OnInit {
appUser: AppUser;
chats: Chat[] = [];
constructor(
private auth: AuthService,
private chatService: ChatService
) {
auth.appUser$.subscribe(appUser => this.appUser = appUser);
// This is where the error is showing up --------------|__ERROR__|
chatService.getAll().valueChanges().subscribe(c => this.chats = c);
}
async ngOnInit() {
}
}
聊天列表.component.html
<li *ngFor="let c of chats" class="list-group-item">
{{ c.name }}
</li>
尝试使用this.db.list<Chat>('/chats').valueChanges();
.
我会在可以帮助检测编译错误时放置返回类型,这将有助于在您的服务中放置返回类型。
getAll(): Observable<Chat[]> {
return this.db.list<Chat>('/chats').valueChanges();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句