Typescript 在AngularFire2中,如何将传入的json转换为对象
我正在使用AngularFire2从谷歌的云Firestore读取数据。演示如何使用Typescript 在AngularFire2中,如何将传入的json转换为对象,typescript,google-cloud-firestore,angularfire2,Typescript,Google Cloud Firestore,Angularfire2,我正在使用AngularFire2从谷歌的云Firestore读取数据。演示如何使用valueChanges()接收每次数据更改时更新的文档集: People类匹配People集合中的我的文档,每个文档都有名字和姓氏。我想调用this.items元素的fullName,例如在html模板中: <ul><li *ngFor="person in people | async">{{person.fullName()}}</li></ul> {{pe
valueChanges()
接收每次数据更改时更新的文档集:
People
类匹配People
集合中的我的文档,每个文档都有名字和姓氏。我想调用this.items
元素的fullName
,例如在html模板中:
<ul><li *ngFor="person in people | async">{{person.fullName()}}</li></ul>
- {{person.fullName()}
为了实现这一点,我订阅了valueChanges并转换了每个传入文档:
this.items = db.collection<People>('people').valueChanges()
.pipe(
map(listOfPeople => {
return listOfPeople.map(person => {
return People.makePersonFrom(person);
});
}),
tap(item => {console.log('Converted', item); } ),
);
this.items=db.collection('people').valueChanges()
.烟斗(
地图(人物列表=>{
返回people.map列表(person=>{
返回人物。makePersonFrom(人物);
});
}),
点击(item=>{console.log('Converted',item);}),
);
我想知道在AngularFire2中是否有更简单的方法来实现这一点。上面的
.pipe
代码主要是样板文件,但用于类名。是否有valueChanges()
的变体可以为我实现这一点 我发现AngularFire2的行为与《指南》HTTP一章的这一小摘录一致:
config: Config;
showConfig() {
this.configService.getConfig()
// clone the data object, using its known Config shape
.subscribe((data: Config) => this.config = { ...data });
它们使用扩展运算符而不是Object.assign
,并且只处理一个响应,但在其他方面是相同的
this.items = db.collection<People>('people').valueChanges()
.pipe(
map(listOfPeople => {
return listOfPeople.map(person => {
return People.makePersonFrom(person);
});
}),
tap(item => {console.log('Converted', item); } ),
);
config: Config;
showConfig() {
this.configService.getConfig()
// clone the data object, using its known Config shape
.subscribe((data: Config) => this.config = { ...data });