(Angularfire) 使用 id 从 firebase 数据库中检索特定数据
(Angularfire) Retrieve specific data from firebase database with id
我正在尝试使用 angular 和 firebase 创建一个 "blog"。我用 Angular 和 Firebase 完成我的第一个项目。
我制作了加载所有条目的首页,然后添加了一个 "read more..." 超链接到特定文章的路径以及条目的 id。
这是 HTML:
<div style="background-color:white" class=" contenedorBlog">
<h1 style="padding-top:100px;" class="text-center mb-4">Pizarrón</h1>
<div *ngFor="let item of items" style="padding:40px" class="rounded">
<h2> {{item.title}} </h2>
<p>Date: {{ item.systemDate }}</p>
<div class="content" [innerHTML]="item.content"></div>
<a [routerLink]="['/article', item.id]">Read more...</a>
</div>
</div>
这里是文章组件的HTML:
<div style="background-color:white" class=" contenedorBlog">
<div style="padding:40px" class="rounded">
<h2> {{items.title}} </h2>
<p>Fecha: {{ items.systemDate }}</p>
<div class="content" [innerHTML]="items.content"></div>
</div>
</div>
我想知道如何调用路线上具有 id 的特定对象 (article/:id)。
这是我目前所拥有的 (article.component.ts)
export class ArticlesComponent implements OnInit {
items:any;
constructor(private blogService: UploadService, private route: ActivatedRoute) {
this.route.params.subscribe(params=> {
this.items=this.blogService.getObjectById(params['id']);
});
}
ngOnInit() {
}
}
问题是...我不知道下一步该做什么。
我正在调用一个函数 insde 一个服务,我只想检索包含路由上的 id 的对象。
getObjectById(id) {
///Help
}
非常感谢,抱歉我的英语不是很好。
在你的构造函数中定义一个 AngularFirestore 变量,然后像这样在你的函数中使用它:
constructor(private afs: AngularFirestore){
this.route.params.subscribe(params=> {
this.getObjectById(params['id']);
});
}
getObjectById(id) {
return this.afs.collection('collection_name')
.doc(id)
.ref;
}
然后您可以在您的组件上使用该功能,例如:
this.blogService.getObjectById(params['id']).get().then(res => {
console.log(res)
})
变化:
this.items=this.blogService.getObjectById(params['id']);
至:
this.blogService.getObjectById(params['id']).subscribe( i => {
this.items = i;
})
他们在 getObjectById(id) 中:
getObjectById(id) {
return this.afs.collection('collectionName').doc(id).valueChanges()
}
如果您只想要一次该值,请在“.subscribe”之前使用“.take(1)”
如果您不想看到 "undefined" 错误,您还需要将 *ngIf="items" 放在显示 "items" 的元素中。
我正在尝试使用 angular 和 firebase 创建一个 "blog"。我用 Angular 和 Firebase 完成我的第一个项目。
我制作了加载所有条目的首页,然后添加了一个 "read more..." 超链接到特定文章的路径以及条目的 id。 这是 HTML:
<div style="background-color:white" class=" contenedorBlog">
<h1 style="padding-top:100px;" class="text-center mb-4">Pizarrón</h1>
<div *ngFor="let item of items" style="padding:40px" class="rounded">
<h2> {{item.title}} </h2>
<p>Date: {{ item.systemDate }}</p>
<div class="content" [innerHTML]="item.content"></div>
<a [routerLink]="['/article', item.id]">Read more...</a>
</div>
</div>
这里是文章组件的HTML:
<div style="background-color:white" class=" contenedorBlog">
<div style="padding:40px" class="rounded">
<h2> {{items.title}} </h2>
<p>Fecha: {{ items.systemDate }}</p>
<div class="content" [innerHTML]="items.content"></div>
</div>
</div>
我想知道如何调用路线上具有 id 的特定对象 (article/:id)。
这是我目前所拥有的 (article.component.ts)
export class ArticlesComponent implements OnInit {
items:any;
constructor(private blogService: UploadService, private route: ActivatedRoute) {
this.route.params.subscribe(params=> {
this.items=this.blogService.getObjectById(params['id']);
});
}
ngOnInit() {
}
}
问题是...我不知道下一步该做什么。 我正在调用一个函数 insde 一个服务,我只想检索包含路由上的 id 的对象。
getObjectById(id) {
///Help
}
非常感谢,抱歉我的英语不是很好。
在你的构造函数中定义一个 AngularFirestore 变量,然后像这样在你的函数中使用它:
constructor(private afs: AngularFirestore){
this.route.params.subscribe(params=> {
this.getObjectById(params['id']);
});
}
getObjectById(id) {
return this.afs.collection('collection_name')
.doc(id)
.ref;
}
然后您可以在您的组件上使用该功能,例如:
this.blogService.getObjectById(params['id']).get().then(res => {
console.log(res)
})
变化:
this.items=this.blogService.getObjectById(params['id']);
至:
this.blogService.getObjectById(params['id']).subscribe( i => {
this.items = i;
})
他们在 getObjectById(id) 中:
getObjectById(id) {
return this.afs.collection('collectionName').doc(id).valueChanges()
}
如果您只想要一次该值,请在“.subscribe”之前使用“.take(1)”
如果您不想看到 "undefined" 错误,您还需要将 *ngIf="items" 放在显示 "items" 的元素中。