How can I set a default route in my @Routes route metadata collection? If you use the angular2 router from @angular/router-deprecated you define the routes in @routeConfig
according to documentation you should just
{ path: '**', component: DefaultLayoutComponent }
on your app-routing.module.ts source: https://angular.io/guide/router
I faced same issue apply all possible solution but finally this solve my problem
export class AppRoutingModule {
constructor(private router: Router) {
this.router.errorHandler = (error: any) => {
this.router.navigate(['404']); // or redirect to default route
}
}
}
Hope this will help you.
Suppose you want to load RegistrationComponent on load and then ConfirmationComponent on some event click on RegistrationComponent.
So in appModule.ts, you can write like this.
RouterModule.forRoot([
{
path: '',
redirectTo: 'registration',
pathMatch: 'full'
},
{
path: 'registration',
component: RegistrationComponent
},
{
path : 'confirmation',
component: ConfirmationComponent
}
])
OR
RouterModule.forRoot([
{
path: '',
component: RegistrationComponent
},
{
path : 'confirmation',
component: ConfirmationComponent
}
])
is also fine. Choose whatever you like.
Only you need to add other parameter in your route, the parameter is useAsDefault:true. For example, if you want the DashboardComponent as default you need to do this:
@RouteConfig([
{ path: '/Dashboard', component: DashboardComponent , useAsDefault:true},
.
.
.
])
I recomend you to add names to your routes.
{ path: '/Dashboard',name:'Dashboard', component: DashboardComponent , useAsDefault:true}
V2.0.0 and later
See also see https://angular.io/guide/router#the-default-route-to-heroes
RouterConfig = [
{ path: '', redirectTo: '/heroes', pathMatch: 'full' },
{ path: 'heroes', component: HeroComponent,
children: [
{ path: '', redirectTo: '/detail', pathMatch: 'full' },
{ path: 'detail', component: HeroDetailComponent }
]
}
];
There is also the catch-all route
{ path: '**', redirectTo: '/heroes', pathMatch: 'full' },
which redirects "invalid" urls.
V3-alpha (vladivostok)
Use path / and redirectTo
RouterConfig = [
{ path: '/', redirectTo: 'heroes', terminal: true },
{ path: 'heroes', component: HeroComponent,
children: [
{ path: '/', redirectTo: 'detail', terminal: true },
{ path: 'detail', component: HeroDetailComponent }
]
}
];
RC.1 @angular/router
The RC router doesn't yet support useAsDefault. As a workaround you can navigate explicitely.
In the root component
export class AppComponent {
constructor(router:Router) {
router.navigate(['/Merge']);
}
}
for other components
export class OtherComponent {
constructor(private router:Router) {}
routerOnActivate(curr: RouteSegment, prev?: RouteSegment, currTree?: RouteTree, prevTree?: RouteTree) : void {
this.router.navigate(['SomeRoute'], curr);
}
}
In Angular 2+, you can set route to default page by adding this route to your route module. In this case login is my target route for the default page.
{path:'',redirectTo:'login', pathMatch: 'full' },