我正在尝试读取json github服务,但出现错误NullInjectorError:没有Http的提供程序!
我已经在所有代码中添加了提供程序,但是它无法正常工作,我不知道是什么导致了错误,但是知道它在哪里发生,我需要一些可以帮助理解导致此错误的原因的人
import { Injectable } from '@angular/core';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule, Http, JsonpModule} from '@angular/http';
import { Observable} from 'rxjs';
import 'rxjs/add/operator/map';
@NgModule({
imports: [
BrowserModule,
HttpModule,
JsonpModule,
]
})
@Injectable({
providedIn:'root'
})
export class GithubService{
constructor(private http:Http){
}
getUser(){
const searchText ="js";
const url = "http://api.github.com/search/users?q="+searchText;
this.http.get(url).subscribe(
res=> {
const data=res.json();
console.log(data);
return data;
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
AppComponent.html:7 ERROR NullInjectorError: StaticInjectorError(AppModule)[GithubService -> Http]:
StaticInjectorError(Platform: …Run Code Online (Sandbox Code Playgroud)