Nomad Nomad - 3 months ago 30
TypeScript Question

Angular 2 RC5 EXCEPTION: Error: Uncaught (in promise): Cannot resolve component using 'HomeComponent'

i am trying my routing feature of abgular2 RC5, Please have look at below code.

app.component.ts

import { Component,HostBinding } from '@angular/core';
import { ROUTER_DIRECTIVES } from "@angular/router";

@Component({
selector: 'my-app',
template: `
<h1>My First Angular 2 App </h1>
<router-outlet></router-outlet>
`
})
export class AppComponent {}

import { Component } from '@angular/core';


usercomp.ts

@Component({
selector: 'user-comp',
template: `
<h1>USER COMPONENT</h1>
`
})
export class UserComponent {}


homecomponent.ts

import { Component } from '@angular/core';

@Component({
moduleId: module.id,
selector: 'home-component',
template: `
<h1>Home Component</h1>
`
})
export class HomeComponent {}


app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { Component } from '@angular/core';
import { AppComponent } from './app.component';
import { routing } from './approuter'
import { ROUTER_DIRECTIVES } from '@angular/router'
import { HomeComponent } from './homecomponent'
import { UserComponent } from './usercomp'

@NgModule({
imports: [ BrowserModule,routing ],
declarations: [ AppComponent,HomeComponent,UserComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }


approuter.ts

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

const APP_ROUTES:Routes = [
{
path:'user',
component:'UserComponent'
},
{
path:'',
component:'HomeComponent'
}

];

export const routing = RouterModule.forRoot(APP_ROUTES);


ERROR

enter image description here

I tried exporting HomeComponent and UserComponent home component everywhere but unable to get rid of this code. With RC4 it worked fine the older way but not sure what wrong i did here, please have a look and let me know when i have mistaken..

Answer
   import { RouterModule,Routes } from '@angular/router'
   import { HomeComponent } from './homecomponent';
   import { UserComponent } from './usercomp';

   const APP_ROUTES:Routes = [
       {
           path:'user',
           component: UserComponent
       },
       {
           path:'',
           component: HomeComponent
       }
   ];

    export const routing = RouterModule.forRoot(APP_ROUTES);

component accepts any component type, check class reference here