首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Get()来查看以角表示的响应

如何使用Get()来查看以角表示的响应
EN

Stack Overflow用户
提问于 2022-07-14 18:48:21
回答 2查看 47关注 0票数 0

我很难在页面上实现API调用,并且想知道订阅/观察方法出了什么问题,现在我有以下代码:

代码语言:javascript
复制
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正在加载,有人能帮上忙吗?

EN

回答 2

Stack Overflow用户

发布于 2022-07-14 18:52:18

代码语言:javascript
复制
  this.getCategories().subscribe(res=>{console.log('GetCategories Resoibse',res)})
票数 0
EN

Stack Overflow用户

发布于 2022-07-14 19:55:12

我建议阅读rxjs和可观察的工作原理,因为您将经常在角度上使用它们。在您的特定示例中,您需要执行以下操作才能订阅:

代码语言:javascript
复制
ngOnInit() {
    this.getCategories().subscribe((data) => {
        // data contains your API response
    });
}

或者,如果你更喜欢与承诺合作,你可以将你的可观察到的承诺转化为承诺,如下所示:

代码语言:javascript
复制
import { lastValueFrom } from 'rxjs';

//...

ngOnInit() {
    lastValueFrom(this.getCategories()).then((data) => {
        // data contains your API response
    });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72985353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档