Restrict unauthorised access in Component Angular

2019-07-02 16:41发布

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,

1条回答
狗以群分
2楼-- · 2019-07-02 17:20

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.

查看更多
登录 后发表回答