我已经创建了一个 AuthGuard 服务并为其实现了 CanLoad 接口,如下所示
import { Injectable } from '@angular/core';
import { CanLoad, Router } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanLoad {
constructor(private router: Router) { }
canLoad() {
if (localStorage.getItem('isLoggedin')) {
return true;
}
this.router.navigate(['/auth/login']);
return false;
}
}
下面是我的应用路由模块
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AuthGuard } from './shared/guard/auth.guard';
const routes: Routes = [
{ path: '', loadChildren: './layout/layout.module#LayoutModule', canLoad: [AuthGuard] },
{ path: 'auth', loadChildren: './auth/auth.module#AuthModule' },
{ path: 'not-found', loadChildren: './not-found/not-found.module#NotFoundModule' },
{ path: '**', redirectTo: 'not-found' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
当我检查浏览器网络选项卡时没有下载文件,我看到一个空白的白页,下面是我在浏览器中看到的警告
platform-browser.js:613 Throttling navigation to prevent the browser from hanging. See https://crbug.com/882238. Command line switch --disable-ipc-flooding-protection can be used to bypass the protection