我正在尝试为我的根url实现基于角色的路由。例如,当用户登录时,我可以将他从login.component重定向到用户的仪表板页面。同样适用于管理员,也可以通过登录重定向到管理员仪表板页面。但若用户打开根url,如何使用角色重定向到特定的仪表板?
认可
export const AppRoutes: Routes = [
{
path: '',
redirectTo: 'dashboard',
pathMatch: 'full'
},
{
path: '',
component: AdminLayoutComponent,
canActivate: [AuthGuard],
canLoad: [AuthGuard],
children: [
{
path: 'dashboard',
loadChildren: './dashboard/dashboard.module#DashboardModule'
},
export const DashboardRoutes: Routes = [
{
path: '',
component: DashboardRedirectComponent,
},
测试仪表板重定向组件:
export class DashboardRedirectComponent implements OnInit, AfterViewInit {
constructor(private auth: AuthenticationService, private router: Router) {
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
if (this.auth.loggedIn()) {
if (currentUser['role'] == 'User') {
this.router.navigate(['/dashboard/user']);
}
if (currentUser['role'] == 'Admin') {
this.router.navigate(['/dashboard/admin']);
}
}
}
我曾尝试使用防护甚至解析器来实现这一点,但没有成功。当我打开我的应用程序的根页面时,它会导航到仪表板,几秒钟后会导航到相应的仪表板页面,但我希望立即导航到用户,而不需要额外的组件。有什么建议吗?