2

In my application I have routes exposed from packages (node modules). So i couldn't use canActivate or canDeactivate for the routes defined inside node modules.

So I started subscribing to all route change in my app component and based on condition i am redirecting the user to different routes.

Since the condition has API call, the route change is not paused in the navigationStart instead it completes the routing to other page and once API call is successful the redirect happens to the other route.

How can i handle this? Is there anyway to pause the route change in navigationStart and enable the route after the API call or is there any other way to define canActivate for all routes including the routes from node modules.

Keshav1007
  • 605
  • 2
  • 8
  • 21
  • I run into the same issue, and im just try to get a workaround for. – Twois Oct 11 '18 at 13:15
  • Actually Im sure, the canActivate wont work, because guards get called after RoutesRecognized, witch means, if you have no route for that, it will redirect to a fallback url, and then only the fallback's guards will be called – Twois Oct 11 '18 at 13:25

1 Answers1

0

Yes, by using resolver you can achieve this,

  1. You need to create resolver service first which will implement "Resolve" interface.

  2. Inside the resolve function, make the API call.

  3. And add the service inside the route declarations like this => resolve: {data: ResolveService}

  4. So, until the API call is made and it doesn't return the data, it won't start the navigationStart Please have a look at this article https://codeburst.io/understanding-resolvers-in-angular-736e9db71267

Sumit Vekariya
  • 482
  • 2
  • 12
  • How will you add the resolve for the routes defined inside some package which are exposed?? since we cannot change the packages we will not be able to add this resolve in the route declaration. Is there any way to add resolve for all the routes by subscribing – Keshav1007 Oct 12 '18 at 06:27
  • I think, the bigger problem is, the resolvers belongs to routes, but if a user try to get an non existing page, the angulra will find a fallback url, and we have no idea about the url what used by the user (but unfortunately it need to resolve the right component). – Twois Oct 12 '18 at 09:05
  • @Keshav1007 are you the author of that npm package? In that case, you can create an resolver, and use them. – Twois Oct 12 '18 at 09:07