Modifying the Menu

The menu is inside the ApplicationLayoutComponent in the @abp/ng.theme.basic package. There are several methods for modifying the menu elements. This document covers these methods. If you would like to replace the menu completely, please refer to Component Replacement documentation and learn how to replace a layout.

How to Add a Logo

The logoUrl property in the environment variables is the url of the logo.

You can add your logo to src/assets folder and set the logoUrl as shown below:

  1. export const environment = {
  2. // other configurations
  3. application: {
  4. name: 'MyProjectName',
  5. logoUrl: 'assets/logo.png',
  6. },
  7. // other configurations
  8. };

How to Add a Navigation Element

Via RoutesService

You can add routes to the menu by calling the add method of RoutesService. It is a singleton service, i.e. provided in root, so you can inject and use it immediately.

  1. import { RoutesService, eLayoutType } from '@abp/ng.core';
  2. import { Component } from '@angular/core';
  3. @Component(/* component metadata */)
  4. export class AppComponent {
  5. constructor(routes: RoutesService) {
  6. routes.add([
  7. {
  8. path: '/your-path',
  9. name: 'Your navigation',
  10. order: 101,
  11. iconClass: 'fas fa-question-circle',
  12. requiredPolicy: 'permission key here',
  13. layout: eLayoutType.application,
  14. },
  15. {
  16. path: '/your-path/child',
  17. name: 'Your child navigation',
  18. parentName: 'Your navigation',
  19. order: 1,
  20. requiredPolicy: 'permission key here',
  21. },
  22. ]);
  23. }
  24. }

An alternative and probably cleaner way is to use a route provider. First create a provider:

  1. // route.provider.ts
  2. import { RoutesService, eLayoutType } from '@abp/ng.core';
  3. import { APP_INITIALIZER } from '@angular/core';
  4. export const APP_ROUTE_PROVIDER = [
  5. { provide: APP_INITIALIZER, useFactory: configureRoutes, deps: [RoutesService], multi: true },
  6. ];
  7. function configureRoutes(routes: RoutesService) {
  8. return () => {
  9. routes.add([
  10. {
  11. path: '/your-path',
  12. name: 'Your navigation',
  13. requiredPolicy: 'permission key here',
  14. order: 101,
  15. iconClass: 'fas fa-question-circle',
  16. layout: eLayoutType.application,
  17. },
  18. {
  19. path: '/your-path/child',
  20. name: 'Your child navigation',
  21. parentName: 'Your navigation',
  22. requiredPolicy: 'permission key here',
  23. order: 1,
  24. },
  25. ]);
  26. };
  27. }

…and then in app.module.ts…

  1. import { NgModule } from '@angular/core';
  2. import { APP_ROUTE_PROVIDER } from './route.provider';
  3. @NgModule({
  4. providers: [APP_ROUTE_PROVIDER],
  5. // imports, declarations, and bootstrap
  6. })
  7. export class AppModule {}

Here is what every property works as:

  • path is the absolute path of the navigation element.
  • name is the label of the navigation element. A localization key or a localization object can be passed.
  • parentName is a reference to the name of the parent route in the menu and is used for creating multi-level menu items.
  • requiredPolicy is the permission key to access the page. See the Permission Management document
  • order is the order of the navigation element. “Administration” has an order of 100, so keep that in mind when ordering top level menu items.
  • iconClass is the class of the i tag, which is placed to the left of the navigation label.
  • layout defines in which layout the route will be loaded. (default: eLayoutType.empty)
  • invisible makes the item invisible in the menu. (default: false)

Via routes Property in AppRoutingModule

You can define your routes by adding routes as a child property to data property of a route configuration in the app-routing.module. The @abp/ng.core package organizes your routes and stores them in the RoutesService.

You can add the routes property like below:

  1. {
  2. path: 'your-path',
  3. data: {
  4. routes: {
  5. name: 'Your navigation',
  6. order: 101,
  7. iconClass: 'fas fa-question-circle',
  8. requiredPolicy: 'permission key here',
  9. children: [
  10. {
  11. path: 'child',
  12. name: 'Your child navigation',
  13. order: 1,
  14. requiredPolicy: 'permission key here',
  15. },
  16. ],
  17. },
  18. },
  19. }

Alternatively, you can do this:

  1. {
  2. path: 'your-path',
  3. data: {
  4. routes: [
  5. {
  6. path: '/your-path',
  7. name: 'Your navigation',
  8. order: 101,
  9. iconClass: 'fas fa-question-circle',
  10. requiredPolicy: 'permission key here',
  11. },
  12. {
  13. path: '/your-path/child',
  14. name: 'Your child navigation',
  15. parentName: 'Your navigation',
  16. order: 1,
  17. requiredPolicy: 'permission key here',
  18. },
  19. ] as ABP.Route[], // can be imported from @abp/ng.core
  20. },
  21. }

The advantage of the second method is that you are not bound to the parent/child structure and use any paths you like.

After adding the routes property as described above, the navigation menu looks like this:

navigation-menu-via-app-routing

How to Patch or Remove a Navigation Element

The patch method of RoutesService finds a route by its name and replaces its configuration with the new configuration passed as the second parameter. Similarly, remove method finds a route and removes it along with its children.

  1. // this.routes is instance of RoutesService
  2. // eThemeSharedRouteNames enum can be imported from @abp/ng.theme.shared
  3. const dashboardRouteConfig: ABP.Route = {
  4. path: '/dashboard',
  5. name: '::Menu:Dashboard',
  6. parentName: '::Menu:Home',
  7. order: 1,
  8. layout: eLayoutType.application,
  9. };
  10. const newHomeRouteConfig: Partial<ABP.Route> = {
  11. iconClass: 'fas fa-home',
  12. parentName: eThemeSharedRouteNames.Administration,
  13. order: 0,
  14. };
  15. this.routes.add([dashboardRouteConfig]);
  16. this.routes.patch('::Menu:Home', newHomeRouteConfig);
  17. this.routes.remove(['Your navigation']);
  • Moved the Home navigation under the Administration dropdown based on given parentName.
  • Added an icon to Home.
  • Specified the order and made Home the first item in list.
  • Added a route named Dashboard as a child of Home.
  • Removed Your navigation along with its child route.

After the operations above, the new menu looks like below:

navigation-menu-after-patching

How to Add an Element to Right Part of the Menu

You can add elements to the right part of the menu by calling the addItems method of NavItemsService. It is a singleton service, i.e. provided in root, so you can inject and use it immediately.

  1. import { NavItemsService } from '@abp/ng.theme.shared';
  2. import { Component } from '@angular/core';
  3. @Component({
  4. template: `
  5. <input type="search" placeholder="Search" class="bg-transparent border-0 color-white" />
  6. `,
  7. })
  8. export class MySearchInputComponent {}
  9. @Component(/* component metadata */)
  10. export class AppComponent {
  11. constructor(private navItems: NavItemsService) {
  12. navItems.addItems([
  13. {
  14. id: 'MySearchInput',
  15. order: 1,
  16. component: MySearchInputComponent,
  17. },
  18. {
  19. id: 'SignOutIcon',
  20. html: '<i class="fas fa-sign-out-alt fa-lg text-white m-2"><i>',
  21. action: () => console.log('Clicked the sign out icon'),
  22. order: 101, // puts as last element
  23. },
  24. ]);
  25. }
  26. }

This inserts a search input and a sign out icon to the menu. The final UI looks like below:

navigation-menu-search-input

The default elements have an order of 100. If you want to place a custom element before the defaults, assign an order number up to 99. If you want to place a custom element after the defaults, assign orders starting from 101. Finally, if you must place an item between the defaults, patch the default element orders as described below. A warning though: We may add another default element in the future and it too will have an order number of 100.

How to Patch or Remove an Right Part Element

The patchItem method of NavItemsService finds an element by its id property and replaces its configuration with the new configuration passed as the second parameter. Similarly, removeItem method finds an element and removes it.

  1. export class AppComponent {
  2. constructor(private navItems: NavItemsService) {
  3. navItems.patchItem(eThemeBasicComponents.Languages, {
  4. requiredPolicy: 'new policy here',
  5. order: 1,
  6. });
  7. navItems.removeItem(eThemeBasicComponents.CurrentUser);
  8. }
  9. }
  • Patched the languages dropdown element with new requiredPolicy and new order.
  • Removed the current user dropdown element.

What’s Next