Using the Router class to navigate - lordoftheflies/angular-essential-training GitHub Wiki

In addition to the router link directive, you can also use the router class to do route navigation. The router class is used from within code rather than from within a template. The media item form submit method currently handles submitting a new item. Let's update that to route to the list upon submission. In the media dash item dash form dot component dot ts file, we import router from the angular router scoped package and then we can add router as a parameter in the constructor using the private access modifier and the type router. And then in the on submit function, we can hand the subscribe call an arrow function to call. And we can use a pair of empty parentheses since we don't need to worry about the parameter, then the arrow, and then the function body. Within the function body, we can call this dot router dot navigate. The router dot navigate method expects a link parameters array. A link parameters array contains the parts needed for router navigation. The route for the media item list by medium needs a base path and it needs the medium route parameter. So we can set up a two element array here and we can set the first item to the base path string of a forward slash and the second item to the new media item dot medium property. Now we can switch over to the browser, navigate to the add form, and add a new media item with the medium set to series and see that we get routed to the appropriate list upon submission.