Top

Routing


One of the most important things while creating an Angular project is creating routes. Without routes you cannot render any page in the browser.

In this page we will learn how to create routes for our pages.

When creating angular project make sure that you select routing to setup router by default.


Defining routes


app-routes.ts file will be created automatically, here we can define our paths for each routes.

But when we are working on a large projects, such as Zono, number of paths will grow very fast, so we create a separate file routes.ts in routes folder to keep our routes more organized.


Routes.ts

import { Routes } from '@angular/router';
{
  path: 'dashboard',
  data: {
      breadcrumb: "Dashboard",
  },  
  loadChildren: () => import('../../../app/component/dashboard/dashboard.routes')
},
{
  path: 'widgets',
  data: {
      breadcrumb: "Widgets",
  },
  loadChildren: () => import('../../../app/component/widgets/widgets.routes')
},

We give the path for our routes, and in loadChildren function we provide our routes .

And then we will import this routes.ts file in ourapp-routes.ts file.


App-routes.ts


 import { Routes } from '@angular/router';
import { ContentComponent } from './shared/component/layout/content/content';
import { dashData } from './shared/routes/routes';

export const routes: Routes = [
...,
{
path: '',
component: Content,
children: dashData,
},
];

Now We are done setting our routes path, but when we create a component inside a routes then path for those components are written in routes_name.routes.ts file. Please refer to Our Create New Page Guide to see how paths for the components are given.