In this article, I am going to show you an excellent Angular Router feature related to dynamic route population. It can be handy both for projects and unit testing. The “Home” component is going to…
Dynamic Routes with AngularPhoto by Edouard Ki on UnsplashIn this article, I am going to show you an excellent Angular Router feature related to dynamic route population. It can be handy both for projects and unit testing.
Preparing the projectFirst, let’s create a minimal project scaffold using the following Angular CLI commands:
ng new medium-dynamic-routes –routing
cd medium-dynamic-routesNext, generate components for a Home and NotFound routes:
ng g component home –module=app
ng g component not-found –module=appThe “Home” component is going to be our landing page. We display “NotFound” component for all the missing content.
To set the initial set of routes, modify the app-routing.module.ts file and add the following entries to the “Routes” collection:
const routes: Routes = [
{ path: ”, component: HomeComponent },
{ path: ‘**’, component: NotFoundComponent }
];I also suggest replacing the content of the main application template app.component.html with a list of links you are going to test. For the moment it is only the “Home” link, but we are going to extend it shortly.
ul
lia you run the application now with the ng serve –open command, you should see the “home works!” label on the main page. That means the default route works as expected, and shows us the auto-generated by Angular CLI content.
By default, the application runs at the http://localhost:4200/ address. Try adding navigating to some “missing” address,…