To navigate imperatively, inject the Router service and call
navigate
or navigateByUrl
on it. Why two methods and not one?
Using router.navigateByUrl
is similar to changing the location bar directly--we are providing the "whole" new URL. Whereas router.navigate
creates a new URL by applying a series of passed-in commands, a patch, to the current URL.
To see the difference clearly, imagine that the current URL is
/inbox/11/messages/22(popup:compose)
.
With this URL, calling
router.navigateByUrl('/inbox/33/messages/44')
will result in
/inbox/33/messages/44
, and calling
router.navigate('/inbox/33/messages/44')
will result in /inbox/33/messages/44(popup:compose)
.
Let's see what we can do with
router.navigate
.