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 cuba, 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
[
  {
      path: 'dashboard',
      loadChildren: () => import('../../components/dashboards/dashboard.routes').then(r => r.dashboard),
      data: {
          title: "Dashboard",
          breadcrumb: "Dashboard"
      },
  },
  {
      path: 'widgets',
      loadChildren: () => import('../../components/widgets/widgets.routes').then(r => r.widgets),
      data: {
          title: "Widgets",
          breadcrumb: "Widgets"
      },
  },
  ...
]

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

And then we will import this routes.ts file in our app.routes.ts file.

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

export const routes: Routes = [
  ...
  {
      path: '',
      component: ContentComponent,
      children: content,
      canActivate: [AdminGuard],
  }
  ...
],

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 route_name.routes.ts file. Please refer to Our Create New Page Guide to see how paths for the components are given.