Typescript/Angular不是函数错误
我不熟悉angular和typescript,在ngOnInit()中调用下面的函数时引发的错误让我感到困惑: TypeError:this.jsCall.getRemoteJASON不是函数。(在 'this.jsCall.getRemoteJASON()','this.jsCall.getRemoteJASON'是 未定义 组件导入一个服务,该服务有助于JSON调用:Typescript/Angular不是函数错误,angular,typescript,Angular,Typescript,我不熟悉angular和typescript,在ngOnInit()中调用下面的函数时引发的错误让我感到困惑: TypeError:this.jsCall.getRemoteJASON不是函数。(在 'this.jsCall.getRemoteJASON()','this.jsCall.getRemoteJASON'是 未定义 组件导入一个服务,该服务有助于JSON调用: import { Component, OnInit } from '@angular/core'; import { JS
import { Component, OnInit } from '@angular/core';
import { JSONService } from './Services/json.service';
import { Observable } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
title = 'Week7Ex1JSON';
studentDetails : string [];
constructor (private jsCall : JSONService){}
ngOnInit(){
this.jsCall.getRemoteJASON().subscribe(data => {
console.log(data);
this.studentDetails = data.students;//retruns an array
console.log(this.studentDetails);
});
}
}
这是服务:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class JSONService {
constructor(private http : HttpClient) { }
getRemoteJASON() : Observable <any>{
return this.http.get("https://jsonblob.com/api/00f6ea4d-1d46-11eb-ab21-8f2b7998e4cf");
}
}
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpClient};
从“rxjs”导入{Observable};
@注射的({
providedIn:'根'
})
导出类JSONService{
构造函数(私有http:HttpClient){}
getRemoteJASON():可观察{
返回此.http.get(“https://jsonblob.com/api/00f6ea4d-1d46-11eb-ab21-8f2b7998e4cf");
}
}
正确的导入和依赖项似乎已就位,因此我无法确定错误的原因
版本:
角度CLI:8.3.8
节点:10.13.0
OS:Darwinx64
欢迎您的任何输入。您能分享您的
app.module.ts
尝试重新启动angular dev服务器吗:ng serve
您的代码对我有效,请尝试重新启动您的dev服务器。感谢重新启动服务器成功-@robert您能发表评论作为答案吗?知道是什么原因吗?请检查是否相关。