我正在使用什么
- 角
- 火库
我想要达到的目标
处理文档数据
将返回的日期转换为可读的内容
是)我有的
我有一个专辑列表,我在其中使用snapshotChanges返回映射数据
我有一张AngularFirestorDocument专辑
问题
- 如何像在专辑列表组件中一样操作专辑组件中的返回日期?
专辑列表
所以这个组件正确地返回了一个专辑列表,我可以在返回它之前操纵日期,以便 HTML 显示正确的格式。
export class AlbumsListCompoment implements OnInit {
private albumCollection: AngularFirestoreCollection<any>;
albumns: Observable<any[]>;
folderId: string;
constructor(
private readonly afs: AngularFirestore,
private activatedRoute: ActivatedRoute,
private router: Router
) {
// Look at the url for the Folder ID and set the local variable
this.activatedRoute.params.forEach((urlParameters) => {
this.folderId = urlParameters['folderId'];
});
// Album Reference "folders/folderid/albums"
this.albumCollection = afs.collection<any>(`folders/${this.folderId}/albums`);
// Get the data
this.albumns = this.albumCollection.snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data();
// Get the date from each album
var albumDateTimeStapm = data.album_date;
// Convert the unix value and format it based on the users locale setting
var albumDateISO = moment(albumDateTimeStapm).format("DD/MM/YYYY");
// Create a new 'name' to use in the HTML binding
data.formattedDate = albumDateISO;
const id = a.payload.doc.id;
return { id, ...data };
});
});
}
ngOnInit() {
}
}
专辑
我并没有完全展示如何接近这个组件来执行相同的操作。它不是一个列表,它是一个文档/专辑。
export class AlbumDetails implements OnInit {
folderId: string;
albumId: string;
private albumDoc: AngularFirestoreDocument<any>;
album: Observable<any>;
constructor(
private readonly afs: AngularFirestore,
private activatedRoute: ActivatedRoute,
private router: Router) {
// Look at the url for the Folder and Album ID and set the local variable
this.activatedRoute.params.forEach((urlParameters) => {
this.folderId = urlParameters['folderId'];
this.albumId = urlParameters['albumId'];
});
this.albumDoc = afs.doc<any>(`folders/${this.folderId}/albums/${this.albumId}`);
this.album = this.albumDoc.valueChanges();
}
ngOnInit() {}
}
任何帮助将不胜感激 :)