我正在做一个Angular项目,同时跟随一个课程。我试图从我的firebase中获取$key,并在路由中使用它来重定向到编辑页面,但我总是“未定义”。我在这里尝试了多种解决方案,但都不起作用。
我的产品服务。
create (product) {
return this.db.list('/products').push(product);
}
getAll() {
return this.db.list('/products').valueChanges();
}
get(productId) : Observable<any> { return this.db.object('/products/' + productId).snapshotChanges();
}
我想使用$key的地方。
<a [routerLink]="['/admin/products/',p.$key]"> Edit </a>
我在浏览器中得到的内容:http://localhost:4200/admin/products/undefined
发布于 2021-05-20 01:24:53
your best bet would be to try as an example like so - set your variable from firebase or firestore as products in your yourcomponent.component.html file:
<div *ngFor="let item of products | keyvalue">
<a [routerLink]="['/admin/products/',item.key]"> Edit </a>
</div>
keyvalue管道是由angular专门用来分段值和键的,并且可以很好地与ngfor配合使用。
https://stackoverflow.com/questions/67607841
复制相似问题