为什么 Angular 要求我提供这些属性,我该如何解决? [英] Why does Angular ask me for these properties, and how can I fix it?
本文介绍了为什么 Angular 要求我提供这些属性,我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
/* Services: client-service.ts */
import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';
import { Observable } from 'rxjs';
import { Client } from '../models/Client';
import 'rxjs/add/operator/map';
@Injectable({
providedIn: 'root'
})
export class ClientService {
clientsCollection!: AngularFirestoreCollection<Client>;
clientDoc!: AngularFirestoreDocument<Client>;
clients!: Observable<Client>[];
client!: Observable<Client>;
constructor(private afs: AngularFirestore) {
this.clientsCollection = this.afs.collection('clients', ref => ref.orderBy('lastName', 'asc'));
}
getClients(): Observable<Client[]> {
// Get clients with the id
this.clients = this.clientsCollection.snapshotChanges().map(changes => {
return changes.map(action => {
const data = action.payload.doc.data() as Client;
data.id = action.payload.doc.id;
return data;
});
});
return this.clients;
}
}
我收到这两个错误:
- 输入'Observable
'缺少类型Observable[]"中的以下属性:length、pop、push、concat 等 24 个. - Observable[]"类型缺少Observable
"类型中的以下属性:_isScalar、源、运算符、提升等.
这是订阅它的组件:
/* the client-component.ts file */
import { Component, OnInit } from '@angular/core';
import { ClientService } from '../../services/client.service';
import { Client } from '../../models/Client';
@Component({
selector: 'app-clients',
templateUrl: './clients.component.html',
styleUrls: ['./clients.component.css']
})
export class ClientsComponent implements OnInit {
/* Properties */
clients!: Client[];
constructor(private clientService: ClientService) { }
ngOnInit(): void {
this.clientService.getClients().subscribe(clients => this.clients = clients);
}
}
推荐答案
您正在对可观察类型进行映射.这需要看起来像这样:
You are doing a map on an observable type. This needs to look something like this instead:
getClients(): Observable<Client[]> {
this.clients = this.clientsCollection.snapshotChanges()
.pipe(
map(c => {
return c.map(a => {
let data = a.payload.doc.data() as Client;
data.id = a.payload.doc.id;
return data;
});
});
return this.clients;
}
这篇关于为什么 Angular 要求我提供这些属性,我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文