やりたいこと
Angularの公式ドキュメントに沿って、アプリを作成しました。
https://angular.jp/tutorial
作成したアプリに対してテストを実施したところ、エラーを吐くのでエラーを解決したいです。
どのようにしたら解決できるでしょうか。
エラー
ng test
を実施すると以下のエラーが出ます。テストプログラムは変えていません。
AppComponent should create the app Failed: Template parse errors: 'router-outlet' is not a known element: 1. If 'router-outlet' is an Angular component, then verify that it is part of this module. 2. If 'router-outlet' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. (" <a routerLink="/heroes">Heroes</a> </nav> [ERROR ->]<router-outlet></router-outlet> <app-messages></app-messages> "): ng:///DynamicTestModule/AppComponent.html@5:0
ソース
おそらくapp.module.tsの@NgModule
が何か悪いのだと思っていますが、当てがつきません。
app.component.html
html
1<h1>{{ title }}</h1> 2<nav> 3 <a routerLink="/dashboard">Dashboard</a> 4 <a routerLink="/heroes">Heroes</a> 5</nav> 6<router-outlet></router-outlet> 7<app-messages></app-messages>
app.component.ts
TypeScript
1import { Component } from '@angular/core'; 2 3@Component({ 4 selector: 'app-root', 5 templateUrl: './app.component.html', 6 styleUrls: ['./app.component.scss'], 7}) 8export class AppComponent { 9 title = 'Tour of Heroes'; 10}
app.module.ts
TypeScript
1import { NgModule } from '@angular/core'; 2import { BrowserModule } from '@angular/platform-browser'; 3import { FormsModule } from '@angular/forms'; // <-- NgModel lives here 4import { HttpClientModule } from '@angular/common/http'; 5 6import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api'; 7import { InMemoryDataService } from './in-memory-data.service'; 8 9import { AppRoutingModule } from './app-routing.module'; 10 11import { AppComponent } from './app.component'; 12import { DashboardComponent } from './dashboard/dashboard.component'; 13import { HeroDetailComponent } from './hero-detail/hero-detail.component'; 14import { HeroesComponent } from './heroes/heroes.component'; 15import { HeroSearchComponent } from './hero-search/hero-search.component'; 16import { MessagesComponent } from './messages/messages.component'; 17 18@NgModule({ 19 imports: [ 20 BrowserModule, 21 FormsModule, 22 AppRoutingModule, 23 HttpClientModule, 24 // The HttpClientInMemoryWebApiModule module intercepts HTTP requests 25 // and returns simulated server responses. 26 // Remove it when a real server is ready to receive requests. 27 HttpClientInMemoryWebApiModule.forRoot( 28 InMemoryDataService, { dataEncapsulation: false }, 29 ), 30 ], 31 declarations: [ 32 AppComponent, 33 DashboardComponent, 34 HeroesComponent, 35 HeroDetailComponent, 36 MessagesComponent, 37 HeroSearchComponent, 38 ], 39 bootstrap: [AppComponent], 40}) 41export class AppModule {}
app-routing.module.ts
TypeScript
1import { NgModule } from '@angular/core'; 2import { RouterModule, Routes } from '@angular/router'; 3 4import { DashboardComponent } from './dashboard/dashboard.component'; 5import { HeroesComponent } from './heroes/heroes.component'; 6import { HeroDetailComponent } from './hero-detail/hero-detail.component'; 7 8const routes: Routes = [ 9 { path: '', redirectTo: '/dashboard', pathMatch: 'full' }, 10 { path: 'dashboard', component: DashboardComponent }, 11 { path: 'detail/:id', component: HeroDetailComponent }, 12 { path: 'heroes', component: HeroesComponent }, 13]; 14 15@NgModule({ 16 imports: [RouterModule.forRoot(routes)], 17 exports: [RouterModule], 18}) 19export class AppRoutingModule {} 20
回答1件
あなたの回答
tips
プレビュー