首页 > 解决方案 > 未从角度调用 Web API

问题描述

我正在尝试以角度调用自定义 API,但由于某种原因它没有调用它。在此之前,我在 Angular 中(在 _api.service.ts 中)制作了 10 个以相同方式调用 Web API 的函数,它们是正确的,我的 Web API 被调用并返回数据,一切都很好。然后我在 web API 中又创建了一个方法,在 angular 中又创建了一个组件(其中调用了函数 getList()),但它没有用,API 永远不会被调用。

getList(id: string, chosen: string): Observable<StopListModel[]> {
        let myUrl: string = API_URL + '/stop/list';
        myUrl += '/' + id;
        myUrl += '/' + chosen;
        //debugger goes to this line, url is correct but API not called
        return this.http.get<StopListModel[]>(myUrl, this.httpUtils.getHTTPHeader());
   }

当我调试时,我看到 API 的 url 是从 angular 正确发送的,就像在我的项目中成功调用 API 的 10 个其他函数一样,所以我认为我不需要从我的 Web API 向您发送代码。我很困惑,所以我将调用 getList() 的行从新组件移动到现有组件,在那里我已经知道我的 _api.service 将成功调用 Web API,因为在该组件中有一个函数(例如 getUsers() )正确调用 API。它没有用,getList() 没有调用 API。

我认为 getList() 的 Web API 有问题,但为了确保在第二个组件中我调用了另一个 _api.service 函数(从另一个组件调用时有效),令人惊讶的是 - 没有为该函数调用 API任何一个。

然后我看到了我认为的问题:当我尝试调用我的任何组件中的任何函数时,所有这些“新”函数从不调用 API,只是已经存在的旧函数,即使它们在它们的正确工作自己的组件。

export class DetaljiStopoviComponent{
            stops$: Observable<StopStatisticsModel[]>;
            list$: Observable<StopListModel[]>
            constructor(
              private apiService: APIService,
              route: ActivatedRoute
            ) {
                var id = route.snapshot.params['id'];
                this.stops$ = this.apiService.getStopStatistics(id); //this works, it was there before - but any other calling I add under this, API is not called (this is the case for every component, every function)
                this.list$ = this.apiService.getList(id, '3'); //this is the newest one, I can't make it call API even in the empty components      
            }
}

我很困惑,在角度方面没有经验,我很想知道问题是什么。我认为 Web API 没有问题,但我不确定。有人有建议吗?

标签: javascriptangularobservable

解决方案


我相信这是一个语法问题,对于get方法试试这个

return this.http.get<StopListModel[]>(myUrl, {headers:this.httpUtils.getHTTPHeader()});

推荐阅读