0

Annahme Return false wird bei der Authentifizierung entweder auf HTTP-Aufruf zurückgegeben werden, aber jetzt bin ich von CanLoadChildren() Methode Inorder zurück canLoad testen Aber trotz Aufruf von canLoad und Rückgabe falscher canLoad lädt immer noch Modul und Weiterleitung zu diesem Modul.kann eine Hilfe sein?Auth Guard ist auf Lazy Loaded Module in angular2 angewendet, aber es ist immer noch Umleitung trotz Ablehnung CanLoad funktioniert nicht

Dank

AuthService

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

@Injectable() 
export class AuthService { 

    constructor() {} 

    public CanLoadChildren(): boolean { 

     return false; 
    } 

} 

Auth Wache Datei

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' 
    } 

] 

Antwort

0

Sie co uld versuchen Sie dies:
Durch die this.router.navigate() in Ihre canload Funktion bewegen sollten Sie in der Lage sein, die Authentifizierung wie folgt zu überprüfen: 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' 
    } 
] 
Verwandte Themen