[英]Ionic 4 - How to pass id parameter to another page and retrieve id's data from external api
in advance sorry for the dummy question, but I can't resolve this problem.提前抱歉这个虚拟问题,但我无法解决这个问题。 So I have a list of categories that comes from a nodeJS api.
所以我有一个来自 nodeJS api 的类别列表。 I want to retrieve the subcategories according to the id, from the database.
我想根据 id 从数据库中检索子类别。 Application is communicating with api through a service.
应用程序正在通过服务与 api 通信。 On the database side, tables "category" and "subcategory" are related through a foreign key which is in "subcategory" table.
在数据库方面,表“类别”和“子类别”通过“子类别”表中的外键关联。 I know my sql request I made on the api is ok because I tested it on postman.
我知道我在 api 上提出的 sql 请求没问题,因为我在 postman 上进行了测试。 The problem is for now, I retrieve all the subcategories, and not only those related to my id's.
现在的问题是,我检索了所有子类别,而不仅仅是那些与我的 id 相关的子类别。
api.service.ts api.service.ts
getDataCategories(): Observable<any> {
return this.http.get(categoriesUrl, httpOptions).pipe(
map(this.extractData),
catchError(this.handleError));
}
getDataSubCategories(): Observable<any> {
return this.http.get(subCategoriesUrl, httpOptions).pipe(
map(this.extractData),
catchError(this.handleError));
}
categoriespage.ts分类页面.ts
export class CategoriesPage implements OnInit {
categories: any=[];
id:any;
constructor(public api: ApiService, private route: ActivatedRoute) { }
ngOnInit() {
this.api.getDataCategories().subscribe(res=>{
this.categories= Object.values(res);
console.log(this.categories);
return this.categories;
})
}
categoriesdetailpage.html分类详情页。html
<ion-col size="6">
<ion-list *ngFor="let data of subCategories[0]">
<ion-item>{{ data.name }}</ion-item>
</ion-list>
</ion-col>
categoriesdetailpage.ts分类详情页.ts
export class CategoryDetailPage implements OnInit {
categoriesdetails:any=[];
constructor(public api: ApiService, private route: ActivatedRoute) { }
ngOnInit() {
this.api.getDataSubCategories().subscribe(res=>{
this.categoriesdetails= Object.values(res);
console.log(this.categoriesdetails);
return this.categoriesdetails;
})
}
Thanks for your help !!谢谢你的帮助 !!
Not sure I understood your question.不确定我是否理解你的问题。 You want to pass an ID to a different page and then use this ID to retrieve the subcategories from the database?
您想将 ID 传递到不同的页面,然后使用此 ID 从数据库中检索子类别? If that is the case you cand do something like this:
如果是这种情况,您可以执行以下操作:
api.service.ts: api.service.ts:
getDataCategories(): Observable<any> {
return this.http.get(categoriesUrl, httpOptions).pipe(
map(this.extractData),
catchError(this.handleError)
);
}
getDataSubCategories(id: string): Observable<any> {
return this.http.get(subCategoriesUrl + "?id_subcategory=" + id, httpOptions).pipe(
map(this.extractData),
catchError(this.handleError)
);
}
categoriespage.ts:类别页面.ts:
import { NavigationExtras } from '@angular/router';
import { NavController } from '@ionic/angular';
export class CategoriesPage implements OnInit {
categories: any=[];
id:any;
constructor(public api: ApiService, private route: ActivatedRoute) { }
ngOnInit() {
this.api.getDataCategories().subscribe(res=>{
this.categories= Object.values(res);
console.log(this.categories);
return this.categories;
});
}
onSelectedCategory(id: any) {
let extras: NavigationExtras = {
state: {
id_category: id
}
};
this.navController.navigateForward('/categorie-detail-page', extras);
}
}
categoriesdetailpage.ts:类别detailpage.ts:
import { Router } from '@angular/router';
export class CategoryDetailPage implements OnInit {
categoriesdetails:any=[];
constructor(private router: Router, public api: ApiService, private route: ActivatedRoute) {
this.state = this.router.getCurrentNavigation().extras.state;
if (this.state != undefined) {
this.api.getSubcategories(this.state['id_category']).subscribe(res => {
this.categoriesdetails= Object.values(res);
console.log(this.categoriesdetails);
});
}
}
}
Of course you have to make a few changes to this code, but I think it can point you into the right direction.当然,您必须对此代码进行一些更改,但我认为它可以为您指明正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.