[英]Lazy load modules conditionally on the basis of HTTP response in Angular 5
I have two roles admin and user. 我有两个角色admin和user。 I have to load the admin page if userId belong to admin and user page if that userId belong to user.
如果userId属于admin,则必须加载管理页面;如果userId属于user,则必须加载用户页面。
The URL is something like http://example.net/[user_id]
. 该URL类似于
http://example.net/[user_id]
。
Is it possible that I can lazy load the modules in Angular routes on the basis of http response I get? 我是否可以根据收到的http响应延迟加载Angular路由中的模块? I hit the API and pass the User Id and get the user role then I have to load the lazy load its module.
我点击了API并传递了用户ID并获得了用户角色,然后必须加载延迟加载其模块。
That's not possible. 那不可能 You can however do a redirect in a guard on the
:user_id
route: 但是,您可以在
:user_id
路由中的防护中进行重定向:
export class UserRoleResolve implements CanActivate {
constructor(private router: Router) {}
public async canActivate(route: ActivatedRouteSnapshot): Promise<boolean> {
const userId: string = route.params.user_id;
const role: string = await this.getRoleFromUser(userId);
if (role === 'admin') {
this.router.navigateByUrl(`/admin/${userId}`);
} else if (role === 'user') {
this.router.navigateByUrl(`/user/${userId}`);
}
}
}
您可以使用以下方法解决您的问题:PreloadingStrategy。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.