0

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..

6
  • please provide implementation for approuter Commented Sep 2, 2016 at 17:38
  • could you add approuter.ts? Commented Sep 2, 2016 at 17:38
  • sorry my bad updated.. Commented Sep 2, 2016 at 17:41
  • @Nomad its just the same as this question Commented Sep 2, 2016 at 18:02
  • @Pankaj Parkar i did pretty good google search though before posting this question, not sure why this one didnt show up.. Commented Sep 2, 2016 at 18:06

2 Answers 2

2
   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

Sign up to request clarification or add additional context in comments.

1 Comment

Okay, did you tried changing your approuter.ts file with above code provided by me?, you are not supposed to give a string for component property as you did in your implementation, you need to give it a component type. Copy paste my code over your code in approuter.ts file
0

I think you need to add pathMatch to your default route:

{ path: '', redirectTo: '/home', pathMatch: 'full'  },
{ path: 'home',  loadChildren: './app/home/home.module#HomeModule' }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.