typescript - 根据用户角色重定向到懒加载模块
在我的 Angular 8 应用程序中,我有 3 个懒加载模块。每个模块针对特定用户的角色。我使用 canActivate 接口进行身份验证和角色守卫。
对于以下路由
{ path : '' , pathMatch : 'full' , redirectTo : ''}
我应该如何使用 redirectTo 属性,以便根据用户角色重定向到懒加载的路由。这是路由模块的代码。
app.routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path : '' , pathMatch: 'full' , redirectTo : ''},
{ path : 'admin' , loadChildren : () => import('./modules/admin/admin.module').then(mod => mod.AdminModule) , canActivate : [AuthGuardService] , data : { role : 'admin'}} ,
{ path : 'member' , loadChildren : () => import('./modules/member/member.module').then(mod => mod.MemberModule) , canActivate : [AuthGuardService] , data : { role : 'member'}} ,
{ path : 'user' , loadChildren : () => import('./modules/user/user.module').then(mod => mod.UserModule) , canActivate : [AuthGuardService] , data : { role : 'user'}} ,
{ path : '**' , component : NotFoundComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
AuthGuardService.ts
import { Injectable } from '@angular/core';
import { Router , CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from '../auth-service/auth-service.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuardService implements CanActivate {
constructor(
private router: Router,
private authService: AuthService
) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) : Observable<boolean> | Promise<boolean> | boolean {
const currentUser = this.authService.userVal;
const role = route.data.role;
if (currentUser) {
if(role && role.indexOf(currentUser.role) > -1)
return true;
else
return false;
}
return false;
}
}
答案#1
再次感谢 @DeborahK。在浏览了这个 Github 讨论之后,这是可行的解决方法。
app.routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path : '' , pathMatch: 'full' , redirectTo : '' , canActivate : [RedirectGuardService]},
{ path : 'admin' , loadChildren : () => import('./modules/admin/admin.module').then(mod => mod.AdminModule) , canActivate : [AuthGuardService] , data : { role : 'admin'}} ,
{ path : 'member' , loadChildren : () => import('./modules/member/member.module').then(mod => mod.MemberModule) , canActivate : [AuthGuardService] , data : { role : 'member'}} ,
{ path : 'user' , loadChildren : () => import('./modules/user/user.module').then(mod => mod.UserModule) , canActivate : [AuthGuardService] , data : { role : 'user'}} ,
{ path : '**' , component : NotFoundComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
redirect-guard.service.ts
import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AuthService } from '../auth-service/auth-service.service';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class RedirectGuardService implements CanActivate {
constructor(
private router: Router,
private authService: AuthService
) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) : Observable<boolean> | Promise<boolean> | boolean {
const user = this.authService.userVal;
if (user) {
this.router.navigate(['/'+ user.role]);
return true;
}
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
return false;
}
}
答案#2
使用这行代码并告诉我。你必须使用路由器构造函数。
this.router.navigate(['site/home');
