Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 setting default route

Am setting my routing in the module and i would like to set default route but it fails

This is the routing module

const appRoutes: Routes = [
 { path: 'login', component: LoginComponent, useAsDefault:true }, //returns an error
 {
  path: 'dash',
  redirectTo:"dashboard"
},

{ path: 'reset-password', component: ResetPasswordComponent },
{ path: '',    redirectTo: '/dashboard', pathMatch: 'full'  },
{ path: '**', component: PageNotFoundComponent }
];

The above returns an error of

LoginComponent; useAsD...' is not assignable to type 'Route[]'

What could be wrong

like image 420
Android coder Avatar asked Feb 10 '17 09:02

Android coder


People also ask

What is default routing in Angular?

Default is "/" (the root path). The path-matching strategy, one of 'prefix' or 'full'. Default is 'prefix'. By default, the router checks URL elements from the left to see if the URL matches a given path and stops when there is a config match.

What does pathMatch Full mean?

pathMatch: 'full' means, that the whole URL path needs to match and is consumed by the route matching algorithm.

How do I enable Angular routing?

Add the AppRoutingModule link The router is dedicated to routing and imported by the root AppModule . By convention, the module class name is AppRoutingModule and it belongs in the app-routing.module.ts in the src/app directory. Run ng generate to create the application routing module.

What does router navigate do?

In Angular, RouterLink is a directive for navigating to a different route declaratively. Router. navigate and Router. navigateByURL are two methods available to the Router class to navigate imperatively in your component classes.


1 Answers

When using useAsDefault you need to have the parent route and the useAsDefault on the child route you want to appear first. So, No need of useAsDefault, You can simply gives Login As Default Routes.And As I see there is no Imported component for Dashboard so,

const appRoutes: Routes = [
  {
    path: '',
    redirectTo: "/login",
    pathMatch: 'full'
  },
  { path: 'login', component: LoginComponent },
  { path: 'reset-password', component: ResetPasswordComponent },
  { path: '**', component: PageNotFoundComponent }
];
like image 99
ER.SHASHI TIWARI Avatar answered Sep 23 '22 21:09

ER.SHASHI TIWARI