3

I'm learning Angular 2 from official tutorial https://angular.io/docs/ts/latest/tutorial/toh-pt5.html. And I have some problem with routing. error message: Type DashboardComponent is part of the declarations of 2 modules: AppRoutingModule and AppModule! I don't know where is my mistake, I think I have everything same as in tutorial.

Error message:

Error message

My code:

AppModule

import { NgModule }       from '@angular/core';
import { BrowserModule }  from '@angular/platform-browser';
import { FormsModule }    from '@angular/forms';

import { AppComponent }         from './app.component';
import { DashboardComponent }   from './dashboard.component';
import { HeroDetailComponent }  from './hero-detail.component';
import { HeroesComponent }      from './heroes.component';
import { HeroService }          from './hero.service';

import { AppRoutingModule }     from './app-routing.module';

@NgModule({
  imports: [
    AppRoutingModule,
    BrowserModule,
    FormsModule,

  ],
  declarations: [
    AppComponent,
    DashboardComponent,
    HeroDetailComponent,
    HeroesComponent
  ],
  providers: [ HeroService ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

App-routing Module

import { NgModule }             from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { DashboardComponent }   from './dashboard.component';
import { HeroesComponent }      from './heroes.component';
import { HeroDetailComponent }  from './hero-detail.component';

const routes: Routes = [
  { path: '', redirectTo: '/dashboard', pathMatch: 'full' },
  { path: 'dashboard',  component: DashboardComponent },
  { path: 'detail/:id', component: HeroDetailComponent },
  { path: 'heroes',     component: HeroesComponent }
];

@NgModule({
  imports: [ RouterModule.forRoot(routes) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule {}

App Component

import {Component} from "@angular/core";
/**
 * Created by lukasfrajt on 13.10.16.
 */


@Component({
  selector: 'my-app',
  template: `
    <h1>{{title}}</h1>
    <nav>
        <a routerLink="/heroes">Heroes</a>
        <a routerLink="/dashboard">Dashboard</a>
    </nav>    
    <router-outlet></router-outlet>`
})
export class AppComponent {
    title = 'Tour of Heroes'
}

Dashboard Component

import {Component} from "@angular/core";
import {Hero} from "./hero";
import {HeroService} from "./hero.service";
import {Router} from "@angular/router";


@Component({
  selector: 'my-dashboard',
  moduleId: module.id,
  templateUrl: `dashboard.component.html`
})
export class DashboardComponent {
    heroes: Hero[] = [];

  constructor(private heroService: HeroService , private  router: Router)
  {

  }
  ngOnInit(): void{
    this.heroService.getHeroes()
      .then(heroes => this.heroes = heroes.slice(1, 5));

  }
  gotoDetail(hero: Hero): void {
    let link = ['/detail', hero.id];
    this.router.navigate(link);
  }
}

Thank you for you help

5
  • will you post your folder structure, index.html, and systemjs.config
    – Bean0341
    Commented Oct 18, 2016 at 17:55
  • Yes of course github.com/coklin3107/Angular_Tutorial/tree/master/Fifth I will be really grateful if you help me @Bean0341
    – Lukas
    Commented Oct 18, 2016 at 18:24
  • All I can find are a few small syntax details but I dont think any are big enough to cause your problem, I have transferred all your code into a plunkr and it works fine :/ the things that you should change though include: In Dashboard Component put you module.id before your selector is called. In your App Component add 'moduleId: module.id' before your selector as well. Also in your index.html put your base href at the very beginning of your '<head tag>'
    – Bean0341
    Commented Oct 18, 2016 at 19:07
  • I did what you said, but still nothin :/ . Maybe could help you the problem starts when I did this part of tutorial Refactor routes to a Routing Module angular.io/docs/ts/latest/tutorial/toh-pt5.html before refactoring everything was ok so maybe same mistake is here, but I don't know..
    – Lukas
    Commented Oct 18, 2016 at 19:54
  • Honestly I know its not what you want to hear but why make your routes module? just leave as a component haha and leave this post up for someone to figure out :)
    – Bean0341
    Commented Oct 18, 2016 at 21:00

2 Answers 2

2

I faced the exactly same problem and the root cause is that the code and angular module version are NOT match. My angular module was RC5 version but I use the commercial sample code. So I just updated the angular version and everything is OK.

2
  • Thank you so much:)
    – Lukas
    Commented Oct 21, 2016 at 14:37
  • How did you find the version of Angular you're working with? I can find the npm version (4.2.0) but not Angular (unless I have a fundamental misunderstanding about what npm means which is certainly possible xD) Commented Feb 15, 2017 at 19:04
1

I had a similar bug halfway through the "Routing" section of the tutorial which ended up being caused by a missing parenthesis, not importing DashboardComponent. Might have been just a "me" mistake, although you are never instructed to add that import statement in the tutorial so it's not inconceivable that someone else would also have this issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.