我正在开发一个项目,该项目正在Angular 8中实现,后端使用Java(Spring Boot 2)。项目假定具有类别和子类别。获取类别没有问题,但我只是不知道如何获得一个子类别。
从Web浏览器的级别来看,我具有以下类别和子类别列表:
[{"categories_id":1,"categoriesName":"Category1","subCategories":
[{"id":1,"subCatName":"subcat1"},{"id":2,"subCatName":"subcat2"},
{"id":4,"subCatName":"subcat3"}],"id":1},
{"categories_id":2,"categoriesName":"Category2","subCategories":
[{"id":3,"subCatName":"subcat3"},{"id":5,"subCatName":"subcat5"}],"id":2},
{"categories_id":3,"categoriesName":"Category3","subCategories":[],"id":3},
{"categories_id":4,"categoriesName":"Category4","subCategories":[],"id":4}]
在Angular中,我创建了一个接口文件CatSubCat.ts,其内容为:
export interface ICatSubCat {
categories_id: number;
id: number;
categoriesName: string;
subCatName: string;
}
Service包含一种从后端获取类别和子类别的方法:
getCategories() : Observable<ICatSubCat[]> {
// getCategories() : any {
return this.http.get<ICatSubCat[]>(this.categories);
}
在
main.component.ts
我声明了变量:
public catsubcat:ICatSubCat[] = [];
public categories_id:number;
public categoriesName:string;
public catId:number;
public subCatName:string;
并从服务订阅了该方法:
catSubCat() {
this.info.getCategories().subscribe (
catsubcat => {
// for (let i=0; catsubcat.length <= 5; i++) {
for (let i in catsubcat) {
// this.categoriesName = catsubcat[0].categoriesName
this.categoriesName = catsubcat[i].categoriesName
this.categories_id = catsubcat[i].categories_id;
this.catId = this.categories_id;
console.log(this.categoriesName);
}
// this.categoriesName = catsubcat[i].categoriesName
}
// console.log(catsubcat);
// console.log(this.categoriesName);
// }
)
}
但是我不知道如何获得一个类别的单个子类别。我可以请您指出我该怎么做吗?
谢谢。