Alireza Valizade Alireza Valizade - 1 month ago 10
Javascript Question

when component complete render then change route in angular 2

I have a

WhatsNew
Component and this my route config:

@RouteConfig([

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

])


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

Answer

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.

Comments