Route Order in Angular - Why Is It Important?



[UPDATE: There is an error on loading of images in every post. I am working to resolve this as soon as possible. Screenshots were mere snap of windows so you can still follow the steps written in text and do the work!] 


In this post, I'll be telling you what is route order and the importance of it.

What are Routes?

- The Router service's Routes represents a route configuration. A single route is defined by a configuration object whereas a set of routes are collected in a Routes array to define a Router configuration. 

- The router attempts to match segments of a given URL against each route by using the configuration options defined in this object. Route supports a variety of route kinds, including static, parametrized, redirect, and wildcard routes, as well as custom route data and resolve methods.

- Typically, the Routes array contains the following properties: path and component.

  • Path: This contains a string and contains URL path to the route. It is the path to match against. It cannot be used together with a custom matcher function. If an user has typed in a URL in his web browser and the resource doesn't exist in your application then you can use a wildcard to redirect them to another page that may say "This page doesn't exist" or something similar. For wildcards, we use '**' as path.
    • The two asterisks, indicate to Angular that this routes definition is a wildcard route. A wildcard route is the last route because it matches any URL.
Component: This contains the component to instantiate when the path matches. This contains the link to the component in ./src/app folder. The name of the component can be found under ./src/app/ <component-name>/<component-name>.component.ts.

Because the Router utilizes a first-match-wins technique for matching routes, the order of the routes is critical. More particular routes should be placed above less specific routes.

So, what's the best way to go about it?

  1. First, list routes with static path,
  2. After that, it's an empty path route, which corresponds to the default route.
  3. The wildcard route should be last since it matches any URL and is only accepted by the Router if no other routes match first.


Comments

Popular

How To Create MySQL Table with SequelizeJS

How To Read CSV Files in Java as Maven Project

How To Create A Hibernate Project in Maven