Angular 4 路由 - 所有路由都重定向到默认路由
Angular 4 Routing - All Routes are redirected to Default Route
我为我的 angular 4 应用程序设置了一个简单的路由,其中包含一个使用 Tetradata Covalent and Angular Material i'm using this Angular 2/4 Authentication tutorial 创建的侧边菜单来处理登录,但是所有子路由器链接都重定向到默认页面(仪表板).任何帮助或建议将不胜感激。
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;
}
}
控制台输出
在定义路由的 app.routing.ts 中,在定义所有路由后将通配符行移动到末尾。
app.routing.ts
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
];
因为它是通配符路线 angular 将在到达任何其他路线之前到达这条路线,因为路线顺序很重要。
您必须将您的代码更改为类似:
export const APP_ROUTES: Routes = [
{ path: '', redirectTo: 'mainpage', pathMatch: 'full' },
{ path: 'login', component: HomeComponent },
{
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: 'mainpage' }
];
我为我的 angular 4 应用程序设置了一个简单的路由,其中包含一个使用 Tetradata Covalent and Angular Material i'm using this Angular 2/4 Authentication tutorial 创建的侧边菜单来处理登录,但是所有子路由器链接都重定向到默认页面(仪表板).任何帮助或建议将不胜感激。
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;
}
}
控制台输出
在定义路由的 app.routing.ts 中,在定义所有路由后将通配符行移动到末尾。
app.routing.ts
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
];
因为它是通配符路线 angular 将在到达任何其他路线之前到达这条路线,因为路线顺序很重要。
您必须将您的代码更改为类似:
export const APP_ROUTES: Routes = [
{ path: '', redirectTo: 'mainpage', pathMatch: 'full' },
{ path: 'login', component: HomeComponent },
{
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: 'mainpage' }
];