0

假设返回false将在http调用上进行身份验证时返回,但现在我从CanLoadChildren()返回false方法Inorder测试canLoad但尽管调用canLoad并返回false canLoad仍然会加载模块并重定向到该模块。任何一个帮助?由于Auth Guard在angular2中的Lazy Loaded模块上应用,但它仍然是重定向尽管拒绝canLoad不起作用

AuthService

import { 
    Injectable 
} from '@angular/core'; 

@Injectable() 
export class AuthService { 

    constructor() {} 

    public CanLoadChildren(): boolean { 

     return false; 
    } 

} 

验证卫队文件

import { 
    AuthService 
} from './auth.service'; 

import { 
    Injectable 
} from '@angular/core'; 

import { 
    CanActivate, 
    CanLoad, 
    Route, 
    ActivatedRouteSnapshot, 
    RouterStateSnapshot, 
    Router 
} from '@angular/router'; 


@Injectable() 
export class CanActivateViaAuthGuardService implements CanActivate, CanLoad { 

    constructor(private _authService: AuthService, private router: Router) {} 

    canLoad(route: Route): boolean { 

     console.log("Can Load Childrens Called"); 
     console.log(route); 
     this.router.navigate(['/Domains']); 
     return this._authService.CanLoadChildren(); 
    } 
} 

app.router.ts

export const APPROUTES: Routes = [ 

    { 
     path: 'User', 

     canLoad: [CanActivateViaAuthGuardService], 

     loadChildren: 'app/LazyAdminConsoleModule' 
    } 

] 

回答

0

您合作ULD试试这个:
通过移动this.router.navigate()到您的canLoad功能,您应该能够验证这样的认证:Auth == true -> Route to /domains

//Authservice 
import { Injectable } from '@angular/core'; 

@Injectable() 
export class AuthService { 

    constructor() { } 

    public CanLoadChildren(): boolean { 
     return false; 
    } 

} 

//Auth Guard 
import { AuthService } from './auth.service'; 
import { Injectable } from '@angular/core'; 
import { CanActivate, CanLoad, Route, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router'; 

@Injectable() 
export class CanActivateViaAuthGuardService implements CanActivate, CanLoad { 

    constructor(private _authService: AuthService, private router: Router) { } 

    canLoad(route: Route): boolean { 
     /*Put this inside of your function*/ 
     if (this._authService.CanLoadChildren() === true) { 
      this.router.navigate(['/Domains']); 
     } else { 
      this.router.navigate(['/'])//HomepageLink here 
     } 
    } 
} 

//App Router 
export const APPROUTES: Routes = [ 
    { 
     path: 'User', canLoad: [CanActivateViaAuthGuardService], loadChildren: 'app/LazyAdminConsoleModule' 
    } 
] 
相关问题