I've setup a simple routing for my angular 4 app that consists of a sidemenu created using Tetradata Covalent and Angular Material i'm using this Angular 2/4 Authentication tutorial to handle login, however all child router links are redirecting to the default page(dashboard). Any help or advice would be highly appreciated.
app.routing.ts
export const APP_ROUTES: Routes = [
{path: 'login', component: HomeComponent},
{ path: '', redirectTo: 'mainpage', pathMatch: 'full' },
{ path: '**', redirectTo: 'mainpage' },
{
path: 'mainpage',
component: MainPageComponent,
canActivate:[AuthGuard],
children: [
{path: 'order', component: OrderComponent},
{path: 'dashboard', component: DashboardComponent},
{path: 'stock', component: StockComponent},
{path: 'Report/sales', component: SalesComponent},
{path: '', redirectTo: 'dashboard', pathMatch: 'full'}
{path: '**', redirectTo: 'dashboard', pathMatch: 'full'}
]
}
];
page.component.html
<td-layout>
<td-navigation-drawer flex sidenavTitle="EzyAgric" logo="assets:logo" name="Akorion" email="info.akorion.com">
<md-nav-list>
<a md-list-item [routerLink]="['dashboard']">
<md-icon>dashboard</md-icon>
Dashboard</a>
<md-divider></md-divider>
<h3 md-subheader>Orders</h3>
<a md-list-item [routerLink]="['stock']">
<md-icon>archive</md-icon>
Stock
</a>
<a md-list-item [routerLink]="['order']">
<md-icon>shopping_cart</md-icon>
Received Orders
</a>
</td-navigation-drawer>
<td-layout-nav [toolbarTitle]="getTitle()" >
<div class="example-scrolling-content">
<router-outlet></router-outlet>
</div>
</td-layout-nav>
</td-layout>
AuthGuard.ts
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (localStorage.getItem('currentUser')) {
// logged in so return true
return true;
}
// not logged in so redirect to login page with the return url
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
return false;
}
}
console output
When the application start, it navigates to the empty route by default. We can configure the router to redirect to a named route by default. So, a redirect route translates the initial relative URL (”) to the desired default path.
Steps to detect route change in Angular application Urls. Import Router, Event, NavigationStart, NavigationEnd, NavigationError from '@angular/router'. And inject router in the constructor. Subscribe to the NavigationStart, NavigationEnd, NavigationError events of the router.
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.
The last route with the path of ** is a wildcard route. The router selects this route if the requested URL doesn't match any of the paths earlier in the list and sends the user to the PageNotFoundComponent .
In your app.routing.ts where you define your routes move the wildcard line to the end after you have defined all your routes.
export const APP_ROUTES: Routes = [
{path: 'login', component: HomeComponent},
{ path: '', redirectTo: 'mainpage', pathMatch: 'full' },
{
path: 'mainpage',
component: MainPageComponent,
canActivate:[AuthGuard],
children: [
{path: 'order', component: OrderComponent},
{path: 'dashboard', component: DashboardComponent},
{path: 'stock', component: StockComponent},
{path: 'Report/sales', component: SalesComponent},
{path: '', redirectTo: 'dashboard', pathMatch: 'full'}
{path: '**', redirectTo: 'dashboard', pathMatch: 'full'}
]
},
{ path: '**', redirectTo: 'mainpage' } // this needs to be after other routes
];
Since it is a wild card route angular will hit this route before hitting any other route since routes order matter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With