Adding a New Page in Angular
This guide will walk you through the steps to add a new page to your Angular application, update the navigation menu, and configure the routing.
Step 1: Update the Navigation Menu
-
Navigate to
data/menuDirectory:- Open the
menu.tsfile located in thedata/menudirectory.
- Open the
-
Add a New Menu Item:
- Add a new entry in the
menusarray. Here’s an example of how to add a new menu item:
export const menus: Navigation[] = [
{
id: 'employee-management',
title: 'Employee Management',
type: 'group',
icon: 'icon-navigation',
children: [
{
id: 'employee-profiles',
title: 'Employee Profiles',
type: 'item',
classes: 'nav-item',
url: '/portal/employee-profiles',
icon: '#custom-story'
},
{
id: 'new-page',
title: 'New Page',
type: 'item',
classes: 'nav-item',
url: '/portal/new-page',
icon: '#new-icon' // Placeholder icon, consult for the appropriate icon
},
],
},
];- Replace
#new-iconwith the appropriate icon. Feel free to consult if you need help selecting one.
- Add a new entry in the
Step 2: Update the Routing Configuration
-
Open
app-routing.module.ts:- Navigate to and open the
app-routing.module.tsfile.
- Navigate to and open the
-
Update the Routes:
- Add a new route for your page within the
routesarray. Here’s how you can set it up:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { PortalLayoutComponent } from './components/layouts/portal/portal-layout/portal-layout.component';
const routes: Routes = [
{
path: 'portal',
component: PortalLayoutComponent,
children: [
{ path: 'employee-profiles', loadChildren: () => import('./pages/employee-profiles/employee-profiles.module').then(m => m.EmployeeProfilesModule) },
{ path: 'new-page', loadChildren: () => import('./pages/new-page/new-page.module').then(m => m.NewPageModule) }, // New Page Route
]
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }- This example assumes that you’ll be lazy-loading the
NewPageModulefor your new page. Replace'new-page'andNewPageModulewith the correct path and module name for your new component.
- Add a new route for your page within the
Step 3: Create the New Page Module and Component
-
Generate the New Module and Component:
- Use Angular CLI to generate the new module and component:
ng generate component pages/new-page- This command will automatically create the necessary files and update the routing for you.
-
Customize the Component:
- Open
new-page.component.tsandnew-page.component.htmlto customize your new page’s logic and template as needed.
- Open
Step 4: Test the New Page
-
Run the Application:
- Start your Angular application:
ng serve -
Verify the Navigation:
- Open your application in the browser, and navigate to
/portal/new-pageto ensure the new page is correctly routed and displayed.
- Open your application in the browser, and navigate to
-
Check the Menu:
- Confirm that the new menu item appears in the navigation and that it routes correctly to your new page.