Following are the fundamental building blocks to creating a route.
AppRoutingModule
into app.config.ts
and add it
to the imports array.
import { ApplicationConfig, importProvidersFrom } from '@angular/core';
import { RouterOutlet, provideRouter } from '@angular/router';
import { App_Route } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideRouter(App_Route),RouterOutlet,provideAnimations(),
importProvidersFrom()
]
};
RouterModule
and Routes
into your routing
module.
The Angular CLI performs this steps automatically. The CLI also set up a Routes
array for your routes and configures the imports
and
exports
array
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { admin, dashboardRoutingModule } from '../../components/dashboards/dashboard.routes';
export const content: Routes = [
{ path: '', children: [
...dashboardRoutingModule.routes,
]}
];
@NgModule({
imports: [RouterModule.forRoot(admin)],
exports: [RouterModule]
})
export class SaredRoutingModule { }
Each route in this array is a JavaScript object that contains two properties. The first property path, defines the URL path for the route. The second property component, defines the component Angular should use for the corresponding path.
Use that component name with path, name, and data attribute in routes array in app-routing.module.ts file
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
export const admin: Routes = [
{path:'default',children:[ {
path: 'default',
loadComponent: () =>
import('./default/default.component').then((m) => m.DefaultComponent),
},]}
];
@NgModule({
imports: [RouterModule.forChild(admin)],
exports: [RouterModule],
})
export class dashboardRoutingModule {
static routes = admin;
}
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Now that you have defined your routes, you can add the component to your
application. First, add links to the components. Assign the anchor tag that you
want to add the route to the routerLink attribute. Set the value of the
attribute to the component to show when a user clicks on each link. Next, update
your component template to include