在调用API并将其用于选项时遇到一些麻烦



我试图调用一个现有的API,返回ID和名称作为一个数字和字符串,并使用ID路由到不同的页面,但不确定我在哪里出错

ngOnInit(): void {
this.getData();
// calling the coach-interaction service getCategories endpoint
// use response to work out the flow based on the the conditionals of the issue
// if categories count = 0 then error
// if (categoryID == 0){
//   this.appService.openSnackBar('You don not have access to this Service');
// }else if (categoryID == 1){
// // if categories count = 1 then set the category id in the payload and route to that category
// } else if (categoryID > 1) {
// // if categories count > 1 then show this component/page
// }
}
goBackTo = () => this.router.navigate([`${appRoutesNames.COACH_INTERACTION}/${coachInteractionRouteNames.MATCH_MAKING}`]).then();

getData = () =>{
this.http.get(this.coachService.GetCategories().subscribe)
.map((res:Response) => (
res.json()
))
.subscribe((data: any) => {console.log(data)})
}

在组件中使用常规函数,而不是箭头函数。this的作用域不同于正则函数和箭头函数

getData() {
this.http.get("YOUR_ENDPOINT")
.subscribe(data => console.log(data))
}
goBackTo() {
this.router.navigate(...)
}

最新更新