0

假设 Return false 将在 http 调用上的身份验证中返回,但现在我从 CanLoadChildren() 方法返回 false 以测试 canLoad 但是尽管调用 canLoad 并返回 false canLoad 仍然加载模块并重定向到该模块。任何人都可以帮助? 谢谢

身份验证服务

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

]
4

1 回答 1

0

你可以试试这个:
通过移动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'
    }
]
于 2017-08-17T13:32:07.160 回答