You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 8.3.17出现StaticInjectorError(AppModule)报错求助

Fixing the StaticInjectorError in Your Angular 8 MEAN Stack App

Hey there, let's get that blank page and StaticInjectorError(AppModule)[DataService -> Http] sorted out! After looking through your code, I can see two straightforward issues causing this problem:

1. Missing HttpModule in Your AppModule

You're using Http from @angular/http in your DataService, but you haven't imported HttpModule into your AppModule's imports array. Angular needs this module to create and inject instances of Http into your service.

2. DataService Not Registered in Providers

Your DataService is marked with @Injectable(), but you haven't added it to the providers array of AppModule. This means Angular doesn't know how to inject it into your AppComponent when it's requested.

Fixed app.module.ts

Here's the corrected version with both fixes applied:

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,
    HttpModule // Added this line to enable Http injection
  ],
  providers: [DataService], // Added this line to register your service
  bootstrap: [AppComponent]
})
export class AppModule { }

Quick heads-up: HttpModule from @angular/http is deprecated in Angular 8. The modern, more robust replacement is HttpClient from @angular/common/http. It automatically parses JSON responses and has better error handling. If you want to update your code to follow best practices, here's how:

Updated data.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'; // Switch to HttpClient
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators'; // Use pipeable operators (Angular 8+ standard)

@Injectable()
export class DataService {
  constructor(private _http: HttpClient) { }

  getAccounts(): Observable<any[]> {
    // HttpClient returns parsed JSON directly, no need for .json()
    return this._http.get("/api/accounts")
      .pipe(map(result => result['data']));
  }
}

Updated app.module.ts (for HttpClient)

Replace HttpModule with HttpClientModule:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { HttpClientModule } from '@angular/common/http'; // Use modern HttpClientModule
import { DataService } from './data.service';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule // Updated this line
  ],
  providers: [DataService],
  bootstrap: [AppComponent]
})
export class AppModule { }

After making these changes, restart your Angular dev server, and your page should load the account data instead of staying blank!

内容的提问来源于stack exchange,提问作者Dallan Byrne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:34:24