Angular 2 rc 1 canActivate

2019-07-14 19:04发布

问题:

Is there some similar like @CanActivate in the new rc router or do I have to use routerOnActivate and navigate back if it is not activable?

Regards

回答1:

The answer as of 15 May 2016 is that @CanActivate functionality is currently not implemented in the release candidate and other than using router-deprecated or switching to an alternate router implementation such as ngrx there really isn't anything to do other than wait.

Here's an open issue asking for a solution: https://github.com/angular/angular/issues/8655

Here is a document which took me ages to find which talks about the proposed router refactorings (you can find mention of @CanActivate in the doc).



回答2:

To use @CanActivate you need to import router-deprecated instead of router.


If you want to use new Router,

there is routerCanDeactivate() lifeCycleHook which is possibly the replacement for @CanActivate.

The only difference would be that, now you'll allow/disallow navigation on previous route instead of the next.