LoginSignup
0
0

More than 3 years have passed since last update.

Angularでサーバ立ち上げ後に検証ツールでStaticInjectorErrorが出る時の対処法

Posted at

Angularで以下のようなコードを書き、

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

ng serveしてlocalhost:4200をブラウザで開き、検証ツールを開いたところ以下のようなエラーが出ました。


StaticInjectorError(AppModule)[RouterOutlet -> ChildrenOutletContexts]: 
  StaticInjectorError(Platform: core)[RouterOutlet -> ChildrenOutletContexts]: 
    NullInjectorError: No provider for ChildrenOutletContexts!

検索すると以下の記事が見つかりました。

1:Import the RouterModule (or any of the "versions" of it generated by forRoot or >forChildren methods) into the module that declares components that use the >directive.
2:The forRoot method of the RouterModule has to be called. Otherwise the services >required by the directive wont be initialized.

と記載してあったので、以下のように編集しました。

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { RouterModule } from '@angular/router';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    RouterModule,
    RouterModule.forRoot([])
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

エラー解消できました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0