Angular的canLoad和canActivate之间的区别? [英] Difference between Angular's canLoad and canActivate?

查看:251
本文介绍了Angular的canLoad和canActivate之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

canLoadcanActivate有什么区别?

export interface Route {
  path?: string;
  pathMatch?: string;
  matcher?: UrlMatcher;
  component?: Type<any>;
  redirectTo?: string;
  outlet?: string;
  canActivate?: any[];
  canActivateChild?: any[];
  canDeactivate?: any[];
  canLoad?: any[];
  data?: Data;
  resolve?: ResolveData;
  children?: Routes;
  loadChildren?: LoadChildren;
}

我什么时候应该选哪个?

When I should which one of them?

推荐答案

canActivate 用于防止未经授权的用户访问某些路由. 请参阅文档以获取更多信息.

canActivate is used to prevent unauthorized users from accessing certain routes. See docs for more info.

canLoad 用于防止未经授权的用户延迟加载整个模块.

canLoad is used to prevent the application from loading entire modules lazily if the user is not authorized to do so.

参阅文档和示例下方以获取更多信息.

See docs and example below for more info.

{
    path: 'admin',
    loadChildren: 'app/admin/admin.module#AdminModule',
    canLoad: [AuthGuard]
},

使用此代码,仅当AuthGuard返回true时,才会将AdminModule的代码加载到应用程序中.

With this code, the code for the AdminModule will only be loaded into the application if AuthGuard returns true.

如果用户无权访问此路由,而我们仅使用了canActivate保护,则即使用户无法访问该路由,也会加载AdminModule.

If the user is not authorized to access this route, and we'd only used a canActivate guard, the AdminModule would be loaded, even though the user would not be able to access that route.

这篇关于Angular的canLoad和canActivate之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆