限制历史状态更改以防止浏览器挂起

Vib*_*bhu 13 javascript browser angular

这是一个初学角度问题.

我的Angular应用程序包含多个功能模块.我通过从angular-cli生成guard来使用authguard,然后在我的app-routing模块中使用CanActivate,如下所示:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AuthGuard } from './auth.guard';

const routes: Routes = [
{path:'login',loadChildren:'./login/login.module#LoginModule',canActivate: 
[AuthGuard]},
{path:'home', loadChildren:'./user/user.module#UserModule',canActivate: 
[AuthGuard]},
{path:'cart', 
loadChildren:'./cart/cart.module#CartModule',canActivate:[AuthGuard]},
 {path:'customer',loadChildren:'./customer/customer.module#CustomerModule',canActivate:[AuthGuard]}
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Run Code Online (Sandbox Code Playgroud)

在我的身份验证中,我写了条件以防止用户访问未经授权的路由:

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from 
'@angular/router';
import { Observable } from 'rxjs/Observable';
import { Router } from '@angular/router';

@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router) { }
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean 
{
  if(["user","customer","cart"].indexOf(localStorage.pass)>=0){alert("auth 
guard!");
return true;}
else 
this.router.navigate(['/login']);
}
}
Run Code Online (Sandbox Code Playgroud)

构建之后,我在重建期间检测到的loadChildren中的重复路径中收到警告警告.我们将检测到最新版本并覆盖它以节省重建时间.您应该执行完整版本以验证您的路由不重叠.

所以我用Google搜索并发现此评论,在将警告添加到最后一条路径后警告消失了.

但之后我登录到我的应用程序并在控制台中显示以下消息:限制历史状态更改以防止浏览器挂起和应用程序卡住.

有什么想法吗?

编辑:我最终通过使用'canLoad'代替'canActivate'来实现它,但如果有人能够提供有关此问题的更多见解,那将会很棒.

小智 28

删除登录路径中的canActivate.这是循环.


Enr*_*ico 6

就我而言,我有一个无限循环。

如果您在路由中使用通配符 (*),请确保它是列表中的最后一个。您应该首先定义所有其他路由。

{ path '/', component: HomeComponent },
{ path 'profile', component: ProfileComponent },
// All your other routes should come first    
{ path: '404', component: NotFoundComponent },
{ path: '**', component: NotFoundComponent }
Run Code Online (Sandbox Code Playgroud)


Nan*_*llu 5

在 Gaurd 中检查其中一条路径是否尝试作为循环加载多次。这是我的问题..