限制组件Angular 2中的未授权访问 [英] Restrict unauthorised access in Component Angular 2

查看:98
本文介绍了限制组件Angular 2中的未授权访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户登录之前,我必须阻止用户沿任何路线行驶。



有人可以帮助我如何在Angular 2组件路由中实现这一目标。 / p>

谢谢

解决方案

您可以使用 @CanActivate 装饰器。您可以使用条件装饰要限制的组件。可以在有角文档中找到更多信息:



https://angular.io/docs/ts/latest/guide/router-deprecated.html#!#lifecycle-hooks



有一个使用名为 @CanDeactivate 的类似修饰符的示例。这是一个很好的起点,可以根据需要进行自定义。


I have to prevent user to move on any route until user is logged in.

Can somebody help me how can we achieve this in Angular 2 Component Routing.

Thanks,

解决方案

You can use the @CanActivate decorator that angular2 provides. You decorate the component that you want to restrict with a condition. More info can be found in the angular docs:

https://angular.io/docs/ts/latest/guide/router-deprecated.html#!#lifecycle-hooks

It has an example using a similar decorator called @CanDeactivate. This is a good starting point, customize it to your needs.

这篇关于限制组件Angular 2中的未授权访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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