Alireza Valizade Alireza Valizade - 3 years ago 123
Javascript Question

when component complete render then change route in angular 2

I have a

Component and this my route config:


{ path: '/whatsnew', name: 'WhatsNew', component: WhatsNew },


I'm in the other route and component. My problem is that I want
to be completely rendered, then route change and navigate to
. I don't want user see the rendering page, how to do it in angular 2?

Answer Source

You can implement the CanActivate lifecycle callback. This way you can either return a promise that completes to true when the call to the server returns that going to that route is fine or redirect to another route.

One problem is that CanActivate is a decorator and DI doesn't support decorators but you need to get a reference to your service to make the server call.

This comment contains a link to a Plunker that demonstrates how DI can be used with CanActivate

There are a few issues that lifecycle callbacks don't wait for the promise to resolve before they continue the navigation. I don't remember if this applies to CanActivate. There is again a new router on its way and it is unlikely that issues will be fixed with the old routers.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download