Routing

Routing

In a single-page application, you change what the user sees by showing or hiding portions of the display that correspond to particular components, rather than going out to the server to get a new page. As users perform application tasks, they need to move between the different views that you have defined.

To handle the navigation from one view to the next, you use the Angular Router. The Router enables navigation by interpreting a browser URL as an instruction to change the view. Your complete route structure is place at app-routing.module.ts file under src » app

Suppose you want to create a new module then you have to add new routes for that modules.


Basic Route

Following are the fundamental building blocks to creating a route.

  1. Import the 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()   
			  ]
			};
			
			
			
			
        

  • Import 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 { }
    		
        
  • Define your routes in your Routes array.
  • 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 { }
            
  • Add your routes to your application.
  • 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 . This element informs Angular to update the application view with the component for the selected route.