我已经为我的 Angular 4 应用程序设置了一个简单的路由,其中包含一个使用Tetradata Covalent 和 Angular Material创建的侧边菜单我正在使用这个Angular 2/4 身份验证教程来处理登录,但是所有子路由器链接都重定向到默认页面(仪表板)。任何帮助或建议将不胜感激。
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;
}
}