Router Tricks Series

Angular: Supercharging The Router

When The Router Does More Than Routing

Lucio Francisco
Wizdm Genesys
Published in
3 min readDec 31, 2019

--

Photo by Shahadat Rahman on Unsplash

Imagine your app has a ‘Contact Us’ link and instead of routing to a dedicated page you’d like a contact form to popup instead.

There would be nothing wrong in hardcoding the ‘Contact Us’ button to do something different than navigating when clicked but wouldn’t it be nice having a way to instruct the router to do that ‘something different’ for us?

The greatest advantage in this would be having a unique way of treating all the links on a toolbar or menu, no matter if they’re expected to really trigger the navigation or do whatever else we need them to.

Let’s see how this can be accomplished.

The ActionLink Observer

The basic idea is to take advantage of the CanActivate guarding mechanism to intercept a routing request:

The code above implements a service named ActionLinkObserver to be used as a routing guard:

--

--

Lucio Francisco
Wizdm Genesys

I believe that whatever problem we’re puzzling ourselves with, once we really get to the bottom of it the solution has to be simple