我很难在页面上实现API调用,并且想知道订阅/观察方法出了什么问题,现在我有以下代码:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { appRoutesNames } from 'src/app/app.routes.names';
import { coachInteractionRouteNames } from '../coach-integration.routes.names';
import { createSessionRouteNames } from '../create-session/create-session.routes.names';
import { coachMatchMakingRouteNames } from './coach-matchmaking.routes.names';
import { CoachGenericResponse,SupportAreaCategory } from './coach-matchmaking.model';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-coach-matchmaking',
templateUrl: './coach-matchmaking.component.html',
styleUrls: ['./coach-matchmaking.component.scss']
})
export class CoachMatchmakingComponent implements OnInit {
appService: any;
coachService: any;
supportAreas: any;
http: any;
constructor(private readonly router: Router, http: HttpClient) { }
ngOnInit(): void {
this.getCategories().subscribe;
}
//API Loading
private userAccessURL = "https://uks-tst-tbp-gw.azurewebsites.net/Business/GetCategories";
getCategories = () => {
return this.http.get(this.userAccessURL);
}
}但是页面的控制台/网络区域中没有任何东西,所以看起来根本没有API正在加载,有人能帮上忙吗?
发布于 2022-07-14 18:52:18
this.getCategories().subscribe(res=>{console.log('GetCategories Resoibse',res)})发布于 2022-07-14 19:55:12
我建议阅读rxjs和可观察的工作原理,因为您将经常在角度上使用它们。在您的特定示例中,您需要执行以下操作才能订阅:
ngOnInit() {
this.getCategories().subscribe((data) => {
// data contains your API response
});
}或者,如果你更喜欢与承诺合作,你可以将你的可观察到的承诺转化为承诺,如下所示:
import { lastValueFrom } from 'rxjs';
//...
ngOnInit() {
lastValueFrom(this.getCategories()).then((data) => {
// data contains your API response
});
}https://stackoverflow.com/questions/72985353
复制相似问题