redirectTo基于用户角色的惰性模块

时间:2019-08-05 10:54:50

标签: typescript routing lazy-loading angular8

在我的angular 8应用程序中,我有3个延迟加载模块。每个模块都针对特定用户的角色。我将canActivate接口用于Auth和Role Guard。
对于以下路线

{ 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;
  }
}

2 个答案:

答案 0 :(得分:1)

使用此行告诉我。您必须使用路由器构造函数。

this.router.navigate(['site/home');

答案 1 :(得分:1)

再次感谢@DeborahK。完成此Github Disucssion之后,这是可行的解决方法。

app.routing.module.ts

awk '{print $2,$6-$13}'

redirect-guard.service.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 { }