【发布时间】:2018-07-01 00:44:21
【问题描述】:
我有几个页面引用了 firestore 中的同一个节点,每个页面都从 firestore 节点中提取不同的段。例如,摘要页面可能会显示专辑标题、日期、流派和图像,而另一个页面可能只会显示标题、艺术家和唱片公司。几个问题:
- 是否可以将其中一个 firestore 查询转换为服务?
- 如果是这样,这是否意味着在使用相同服务的不同页面(角度组件)之间导航时只读取一次数据?
- 只有在通过 observable 在 Firestore 中修改数据时,查询才会再次运行? ("return Observable.create(observer => {")
我已尝试使用以下代码提供服务。但是,观察到的问题是在页面刷新时,数据不存在。但是,它在浏览该站点时存在。我相信这是因为我的页面在 observable 返回之前正在运行。有没有办法将查询包装为可观察对象?
任何帮助将不胜感激。
getAlbumData() {
this.albumDoc = this.afs.doc(`albums/${this.albumId}`);
this.album = this.albumDoc.snapshotChanges();
this.album.subscribe((value) => {
// The returned Data
const data = value.payload.data();
// Firebase Reference
var storage = firebase.storage();
// If album cover exists
if (data.project_signature != undefined) {
// Get the Image URL
var image = data.album_cover_image;
// Create an image reference to the storage location
var imagePathReference = storage.ref().child(image);
// Get the download URL and set the local variable to the result (url)
imagePathReference.getDownloadURL().then((url) => {
this.album_cover = url;
});
}
});
}
【问题讨论】:
标签: javascript angular google-cloud-firestore angular-services angular2-observables