February 7, 2023

Angular Routing

Angular Routing
Routing in a single-page application gives a feel to the user that the user will be just seeing HTML upgradation and new data rendering, this is a faster approach for avoiding the timespan of loading a whole new page from the server. Here only the view will be updated and for updating the view from one to another Angular Routing is used. The Router enables navigation by interpreting a browser URL as an instruction to change the view.

Table of Contents

Angular Router
  • Blog-Detail ArrowAngular Router is a powerful Javascript router built and maintained by the Angular Core team and it can be installed by installing  @angular/router package.
  • Blog-Detail ArrowIt provides a complete routing library with the possibility to have multiple router outlets, different path-matching strategies, easy access to route parameters, and route guards to protect components from unauthorized access.
Router Outlet
  • Blog-Detail ArrowThe router-outlet is a directive available from the router Library which helps to change components based on the URL
  • Blog-Detail ArrowA single application can contain multiple router outlets
  • Blog-Detail ArrowFor adding router outlets you need to add the following code in the HTML file
Routes and Paths
  • Blog-Detail ArrowRoutes are comprised of a path and a component attributes
  • Blog-Detail ArrowThe path refers to the part of the URL that determines a unique view that should be displayed, and the component refers to the Angular component that needs to be associated with a path.
  • Blog-Detail ArrowBased on the route definition provided router will navigate the user to a specific view
  • Blog-Detail ArrowEach route maps a URL path to a component
  • Blog-Detail ArrowThe path can be empty which is a default path and is generally used at the start of the application
Wildcard routes
  • Blog-Detail ArrowWildcard routes are defined by string (**)
  • Blog-Detail ArrowThis route will be executed if the requested URL is not defined in the routes
  • Blog-Detail ArrowThese routes are generally used to define Not Found routes
  • Blog-Detail ArrowFor example,
Routes matching strategies
  • Blog-Detail ArrowEach route can have different matching strategies. Default strategies would be just matching routes in the browser URL which is just matching the route path’s prefix.
  • Blog-Detail ArrowFor example, the default path definition can also be written as,
  • Blog-Detail ArrowHere the patchMath attribute specifies the matching strategy. In this case, it’s the prefix that is the default.
  • Blog-Detail ArrowAnother matching strategy is “full”. When it's specified for a route the router will check if the path is exactly equal to the path of the current browser’s URL:
Route Params
  • Blog-Detail ArrowFor passing data between multiple components angular routes will help
  • Blog-Detail ArrowAngular Router allows you to access parameters in different ways:
  • Blog-Detail ArrowActivatedRoute
  • Blog-Detail ArrowParamMap
  • Blog-Detail ArrowFor passing route params you can use colon syntax, For example:
Route Guards
  • Blog-Detail ArrowDevelopers can set the route guards to set a logic whenever a route is accessed.
  • Blog-Detail ArrowThis logic will define whether the user should be able to access the component or not
  • Blog-Detail ArrowFor example, If the developer wants to check if the user is logged in or not at that time route guard will not allow non logged in user to access private pages
  • Blog-Detail ArrowYou can add a route guard by implementing the CanActivate interface available from the @angular/router package and extending the canActivate() method which holds the logic to allow or deny access to the route.
  • Blog-Detail ArrowRoute guard example,
  • Blog-Detail ArrowYou can then protect a route with the guard using the canActivate attribute:
  • Blog-Detail ArrowFor creating navigation links, the angular router provides routerLink directives.
  • Blog-Detail ArrowThis directive takes the path associated with the component to navigate to. For example:
Using multiple router outlets
  • Blog-Detail ArrowUsing an angular router we can add multiple router outlets in a single application
  • Blog-Detail ArrowA component has one associated primary route and can have auxiliary routes.
  • Blog-Detail ArrowAuxiliary routes enable developers to navigate multiple routes at the same time.
  • Blog-Detail ArrowAll outlets should have a name instead of the primary outlet.
  • Blog-Detail ArrowFor example,
  • Blog-Detail ArrowThen you can specify the outlet in the routing file where you want your component to be rendered
Conclusion
This tutorial demonstrated how to use the Angular Router to add routing and navigation to our application. We covered concepts like router-oulet, routes, and paths.

Found this blog useful? Don't forget to share it with your network

Stay Ahead with Our Monthly Newsletter!

Get the latest tech insights, trends, and updates delivered straight to your inbox!

Featured Insights

Team up with us to enhance and

achieve your business objectives

LET'S WORK

TLogoGETHER