Reputation: 1139
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
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..
Upvotes: 0
Views: 747
Reputation: 2676
I think you need to add pathMatch to your default route:
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', loadChildren: './app/home/home.module#HomeModule' }
Upvotes: 0
Reputation: 5891
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
Upvotes: 2