简体   繁体   中英

CanActivate vs redirect in constructor

I've seen a lot of questions about using DI for the @CanActivate decorator. I'm aware of the implementation of a custom Injector to provide the Router .

My question is more about the general design: What are the advantages (or disadvantages) of using @CanActivate over just redirecting inside the equivalent component constructor. Is the last one a bad solution?

CanActivate is especially designed to be completely outside the component. This is why it's difficult to inject the Router . With the constructor a component instance would need to be created and then immediately destroyed because of the redirect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM