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);
  }
}
coollang12345 回答:StaticInjectorError(AppModule)[AppComponent-> DataService]

我发现了问题,我必须将dataService添加到提供程序数组中,并添加HttpModule进行导入。

  @NgModule({
  declarations: [
    AppComponent
  ],imports: [
    BrowserModule,HttpModule,AppRoutingModule
  ],providers: [DataService],bootstrap: [AppComponent]
})
,

您需要像这样在app.module.ts文件的provider部分中添加DataService

MATCH (a:sale) 
SET a.saleDate = date(a.saleDate) 
RETURN a
本文链接:https://www.f2er.com/3134765.html

大家都在问