gpt4 book ai didi

Angular7 : CanLoad Auth guard hangs browser

转载 作者:行者123 更新时间:2023-12-04 17:37:50 29 4
gpt4 key购买 nike

我已经创建了一个 AuthGuard 服务并为其实现了 CanLoad 接口(interface),如下所示

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

最佳答案

问题终于解决了,只需要按如下路线重新排序

const routes: Routes = [
{ path: 'auth', loadChildren: './auth/auth.module#AuthModule' },
{
path: '', loadChildren: './layout/layout.module#LayoutModule', canLoad: [AuthGuard],
},
{ path: 'not-found', loadChildren: './not-found/not-found.module#NotFoundModule' },
{ path: '**', redirectTo: 'not-found' }
];

关于Angular7 : CanLoad Auth guard hangs browser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55919120/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com