StaticInjectorError(AppModule)[AppComponent -> DataService]
StaticInjectorError(AppModule)[AppComponent -> DataService]
我一直在关注关于设置平均堆栈的教程,但是当 运行 我在本地主机上的代码时,我得到一个空白屏幕并显示错误 'StaticInjectorError(AppModule)[DataService -> Http]:'。我花了两天时间试图找出这个错误,但找不到任何东西。
关于我使用的工具的详细信息,Angular 8.3.17,节点 v12
data.service.ts
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class DataService {
result:any;
constructor(private _http: Http) { }
getAccounts() {
return this._http.get("/api/accounts")
.map(result => this.result = result.json().data);
}
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
// Import the Http Module and our Data Service
import { HttpModule } from '@angular/http';
import { DataService } from './data.service';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.ts
import { Component } from '@angular/core';
// Import the DataService
import { DataService } from './data.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
// Define a users property to hold our user data
accounts: Array<any>;
// Create an instance of the DataService through dependency injection
constructor(private _dataService: DataService) {
// Access the Data Service's getUsers() method we defined
this._dataService.getAccounts()
.subscribe(res => this.accounts = res);
}
}
我发现了问题,我必须将 dataService 添加到我的 providers 数组,并将 HttpModule 添加到 imports。
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpModule,
AppRoutingModule
],
providers: [DataService],
bootstrap: [AppComponent]
})
您需要像这样在 app.module.ts 文件的提供程序部分添加数据服务
providers: [DataService],
我一直在关注关于设置平均堆栈的教程,但是当 运行 我在本地主机上的代码时,我得到一个空白屏幕并显示错误 'StaticInjectorError(AppModule)[DataService -> Http]:'。我花了两天时间试图找出这个错误,但找不到任何东西。
关于我使用的工具的详细信息,Angular 8.3.17,节点 v12
data.service.ts
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class DataService {
result:any;
constructor(private _http: Http) { }
getAccounts() {
return this._http.get("/api/accounts")
.map(result => this.result = result.json().data);
}
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
// Import the Http Module and our Data Service
import { HttpModule } from '@angular/http';
import { DataService } from './data.service';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.ts
import { Component } from '@angular/core';
// Import the DataService
import { DataService } from './data.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
// Define a users property to hold our user data
accounts: Array<any>;
// Create an instance of the DataService through dependency injection
constructor(private _dataService: DataService) {
// Access the Data Service's getUsers() method we defined
this._dataService.getAccounts()
.subscribe(res => this.accounts = res);
}
}
我发现了问题,我必须将 dataService 添加到我的 providers 数组,并将 HttpModule 添加到 imports。
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpModule,
AppRoutingModule
],
providers: [DataService],
bootstrap: [AppComponent]
})
您需要像这样在 app.module.ts 文件的提供程序部分添加数据服务
providers: [DataService],