Menu导航菜单

为页面和功能提供导航的菜单列表。

何时使用

导航菜单是一个网站的灵魂,用户依赖导航在各个页面中进行跳转。一般分为顶部导航和侧边导航,顶部导航提供全局性的类目和功能,侧边导航提供多级结构来收纳和排列网站架构。

更多布局和导航的使用可以参考:通用布局

代码演示

Menu导航菜单 - 图1

顶部导航

水平的顶部导航菜单。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-menu-horizontal',
  4. template: `
  5. <ul nz-menu [nzMode]="'horizontal'">
  6. <li nz-menu-item><i nz-icon type="mail"></i> Navigation One</li>
  7. <li nz-menu-item nzDisabled><i nz-icon type="appstore"></i> Navigation Two</li>
  8. <li nz-submenu>
  9. <span title><i nz-icon type="setting"></i> Navigation Three - Submenu</span>
  10. <ul>
  11. <li nz-menu-group>
  12. <span title>Item 1</span>
  13. <ul>
  14. <li nz-menu-item>Option 1</li>
  15. <li nz-menu-item>Option 2</li>
  16. </ul>
  17. </li>
  18. <li nz-menu-group>
  19. <span title>Item 2</span>
  20. <ul>
  21. <li nz-menu-item>Option 3</li>
  22. <li nz-menu-item>Option 4</li>
  23. <li nz-submenu>
  24. <span title>Sub Menu</span>
  25. <ul>
  26. <li nz-menu-item nzDisabled>Option 5</li>
  27. <li nz-menu-item>Option 6</li>
  28. </ul>
  29. </li>
  30. <li nz-submenu nzDisabled>
  31. <span title>Disabled Sub Menu</span>
  32. <ul>
  33. <li nz-menu-item>Option 5</li>
  34. <li nz-menu-item>Option 6</li>
  35. </ul>
  36. </li>
  37. </ul>
  38. </li>
  39. </ul>
  40. </li>
  41. <li nz-menu-item>
  42. <a href="https://ng.ant.design" target="_blank" rel="noopener noreferrer">Navigation Four - Link</a>
  43. </li>
  44. </ul>
  45. `
  46. })
  47. export class NzDemoMenuHorizontalComponent {}

Menu导航菜单 - 图2

内嵌菜单

垂直菜单,子菜单内嵌在菜单区域。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-menu-inline',
  4. template: `
  5. <ul nz-menu [nzMode]="'inline'" style="width: 240px;">
  6. <li nz-submenu>
  7. <span title><i nz-icon type="mail"></i> Navigation One</span>
  8. <ul>
  9. <li nz-menu-group>
  10. <span title>Item 1</span>
  11. <ul>
  12. <li nz-menu-item>Option 1</li>
  13. <li nz-menu-item>Option 2</li>
  14. </ul>
  15. </li>
  16. <li nz-menu-group>
  17. <span title>Item 2</span>
  18. <ul>
  19. <li nz-menu-item>Option 3</li>
  20. <li nz-menu-item>Option 4</li>
  21. </ul>
  22. </li>
  23. </ul>
  24. </li>
  25. <li nz-submenu>
  26. <span title><i nz-icon type="appstore"></i> Navigation Two</span>
  27. <ul>
  28. <li nz-menu-item>Option 5</li>
  29. <li nz-menu-item>Option 6</li>
  30. <li nz-submenu>
  31. <span title>Submenu</span>
  32. <ul>
  33. <li nz-menu-item>Option 7</li>
  34. <li nz-menu-item>Option 8</li>
  35. <li nz-submenu>
  36. <span title>Submenu</span>
  37. <ul>
  38. <li nz-menu-item>Option 9</li>
  39. <li nz-menu-item>Option 10</li>
  40. </ul>
  41. </li>
  42. </ul>
  43. </li>
  44. </ul>
  45. </li>
  46. <li nz-submenu>
  47. <span title><i nz-icon type="setting"></i> Navigation Three</span>
  48. <ul>
  49. <li nz-menu-item>Option 11</li>
  50. <li nz-menu-item>Option 12</li>
  51. <li nz-menu-item>Option 13</li>
  52. </ul>
  53. </li>
  54. </ul>
  55. `,
  56. styles: []
  57. })
  58. export class NzDemoMenuInlineComponent {}

Menu导航菜单 - 图3

缩起内嵌菜单

内嵌菜单可以被缩起/展开。

你可以在 Layout 里查看侧边布局结合的完整示例。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-menu-inline-collapsed',
  4. template: `
  5. <div style="width: 240px;">
  6. <button nz-button [nzType]="'primary'" (click)="toggleCollapsed()" style="margin-bottom: 10px;">
  7. <i nz-icon [type]="isCollapsed ? 'menu-unfold' : 'menu-fold'"></i>
  8. </button>
  9. <ul nz-menu [nzMode]="'inline'" nzTheme="dark" [nzInlineCollapsed]="isCollapsed">
  10. <li nz-menu-item nz-tooltip nzPlacement="right" [nzTitle]="isCollapsed ? 'Navigation One' : ''">
  11. <span title>
  12. <i nz-icon type="mail"></i>
  13. <span>Navigation One</span>
  14. </span>
  15. </li>
  16. <li nz-submenu>
  17. <span title>
  18. <i nz-icon type="appstore"></i>
  19. <span>Navigation Two</span>
  20. </span>
  21. <ul>
  22. <li nz-menu-item>Option 5</li>
  23. <li nz-menu-item>Option 6</li>
  24. <li nz-submenu>
  25. <span title>Submenu</span>
  26. <ul>
  27. <li nz-menu-item>Option 7</li>
  28. <li nz-menu-item>Option 8</li>
  29. </ul>
  30. </li>
  31. </ul>
  32. </li>
  33. <li nz-submenu>
  34. <span title>
  35. <i nz-icon type="setting"></i>
  36. <span>Navigation Three</span>
  37. </span>
  38. <ul>
  39. <li nz-menu-item>Option 9</li>
  40. <li nz-menu-item>Option 10</li>
  41. <li nz-menu-item>Option 11</li>
  42. </ul>
  43. </li>
  44. </ul>
  45. </div>
  46. `
  47. })
  48. export class NzDemoMenuInlineCollapsedComponent {
  49. isCollapsed = false;
  50. toggleCollapsed(): void {
  51. this.isCollapsed = !this.isCollapsed;
  52. }
  53. }

Menu导航菜单 - 图4

只展开当前父级菜单

点击菜单,收起其他展开的所有菜单,保持菜单聚焦简洁。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-menu-sider-current',
  4. template: `
  5. <ul nz-menu [nzMode]="'inline'" style="width: 240px;">
  6. <li nz-submenu [(nzOpen)]="openMap.sub1" (nzOpenChange)="openHandler('sub1')">
  7. <span title><i nz-icon type="mail"></i> Navigation One</span>
  8. <ul>
  9. <li nz-menu-group>
  10. <span title>Item 1</span>
  11. <ul>
  12. <li nz-menu-item>Option 1</li>
  13. <li nz-menu-item>Option 2</li>
  14. </ul>
  15. </li>
  16. <li nz-menu-group>
  17. <span title>Item 2</span>
  18. <ul>
  19. <li nz-menu-item>Option 3</li>
  20. <li nz-menu-item>Option 4</li>
  21. </ul>
  22. </li>
  23. </ul>
  24. </li>
  25. <li nz-submenu [(nzOpen)]="openMap.sub2" (nzOpenChange)="openHandler('sub2')">
  26. <span title><i nz-icon type="appstore"></i> Navigation Two</span>
  27. <ul>
  28. <li nz-menu-item>Option 5</li>
  29. <li nz-menu-item>Option 6</li>
  30. <li nz-submenu>
  31. <span title>Submenu</span>
  32. <ul>
  33. <li nz-menu-item>Option 7</li>
  34. <li nz-menu-item>Option 8</li>
  35. </ul>
  36. </li>
  37. </ul>
  38. </li>
  39. <li nz-submenu [(nzOpen)]="openMap.sub3" (nzOpenChange)="openHandler('sub3')">
  40. <span title><i nz-icon type="setting"></i> Navigation Three</span>
  41. <ul>
  42. <li nz-menu-item>Option 9</li>
  43. <li nz-menu-item>Option 10</li>
  44. <li nz-menu-item>Option 11</li>
  45. </ul>
  46. </li>
  47. </ul>
  48. `,
  49. styles: []
  50. })
  51. export class NzDemoMenuSiderCurrentComponent {
  52. openMap: { [name: string]: boolean } = {
  53. sub1: true,
  54. sub2: false,
  55. sub3: false
  56. };
  57. openHandler(value: string): void {
  58. for (const key in this.openMap) {
  59. if (key !== value) {
  60. this.openMap[key] = false;
  61. }
  62. }
  63. }
  64. }

Menu导航菜单 - 图5

垂直菜单

子菜单是弹出的形式。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-menu-vertical',
  4. template: `
  5. <ul nz-menu [nzMode]="'vertical'" style="width: 240px;">
  6. <li nz-submenu>
  7. <span title><i nz-icon type="mail"></i> Navigation One</span>
  8. <ul>
  9. <li nz-menu-group>
  10. <span title>Item 1</span>
  11. <ul>
  12. <li nz-menu-item>Option 1</li>
  13. <li nz-menu-item>Option 2</li>
  14. </ul>
  15. </li>
  16. <li nz-menu-group>
  17. <span title>Item 2</span>
  18. <ul>
  19. <li nz-menu-item>Option 3</li>
  20. <li nz-menu-item>Option 4</li>
  21. </ul>
  22. </li>
  23. </ul>
  24. </li>
  25. <li nz-submenu (nzOpenChange)="change($event)">
  26. <span title><i nz-icon type="appstore"></i> Navigation Two</span>
  27. <ul>
  28. <li nz-menu-item>Option 5</li>
  29. <li nz-menu-item>Option 6</li>
  30. <li nz-submenu>
  31. <span title>Submenu</span>
  32. <ul>
  33. <li nz-menu-item>Option 7</li>
  34. <li nz-menu-item>Option 8</li>
  35. </ul>
  36. </li>
  37. </ul>
  38. </li>
  39. <li nz-submenu>
  40. <span title><i nz-icon type="setting"></i> Navigation Three</span>
  41. <ul>
  42. <li nz-menu-item>Option 9</li>
  43. <li nz-menu-item>Option 10</li>
  44. <li nz-menu-item>Option 11</li>
  45. </ul>
  46. </li>
  47. </ul>
  48. `,
  49. styles: []
  50. })
  51. export class NzDemoMenuVerticalComponent {
  52. change(value: boolean): void {
  53. console.log(value);
  54. }
  55. }

Menu导航菜单 - 图6

主题

内建了两套主题 light|dark,默认 light

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-menu-theme',
  4. template: `
  5. <nz-switch [(ngModel)]="theme">
  6. <span checked>Dark</span>
  7. <span unchecked>Light</span>
  8. </nz-switch>
  9. <br />
  10. <br />
  11. <ul nz-menu [nzMode]="'inline'" style="width: 240px;" [nzTheme]="theme ? 'dark' : 'light'">
  12. <li nz-submenu nzOpen>
  13. <span title><i nz-icon type="mail"></i> Navigation One</span>
  14. <ul>
  15. <li nz-menu-group>
  16. <span title>Item 1</span>
  17. <ul>
  18. <li nz-menu-item>Option 1</li>
  19. <li nz-menu-item>Option 2</li>
  20. </ul>
  21. </li>
  22. <li nz-menu-group>
  23. <span title>Item 2</span>
  24. <ul>
  25. <li nz-menu-item>Option 3</li>
  26. <li nz-menu-item>Option 4</li>
  27. </ul>
  28. </li>
  29. </ul>
  30. </li>
  31. <li nz-submenu>
  32. <span title><i nz-icon type="appstore"></i> Navigation Two</span>
  33. <ul>
  34. <li nz-menu-item>Option 5</li>
  35. <li nz-menu-item>Option 6</li>
  36. <li nz-submenu>
  37. <span title>Submenu</span>
  38. <ul>
  39. <li nz-menu-item>Option 7</li>
  40. <li nz-menu-item>Option 8</li>
  41. </ul>
  42. </li>
  43. </ul>
  44. </li>
  45. <li nz-submenu>
  46. <span title><i nz-icon type="setting"></i> Navigation Three</span>
  47. <ul>
  48. <li nz-menu-item>Option 9</li>
  49. <li nz-menu-item>Option 10</li>
  50. <li nz-menu-item>Option 11</li>
  51. </ul>
  52. </li>
  53. </ul>
  54. `,
  55. styles: []
  56. })
  57. export class NzDemoMenuThemeComponent {
  58. theme = true;
  59. }

Menu导航菜单 - 图7

切换菜单类型

展示动态切换模式。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-menu-switch-mode',
  4. template: `
  5. <nz-switch [(ngModel)]="mode"> </nz-switch>
  6. Change Mode
  7. <nz-divider nzType="vertical"></nz-divider>
  8. <nz-switch [(ngModel)]="dark"> </nz-switch>
  9. Change Theme
  10. <br />
  11. <br />
  12. <ul nz-menu [nzMode]="mode ? 'vertical' : 'inline'" [nzTheme]="dark ? 'dark' : 'light'" style="width: 240px;">
  13. <li nz-submenu>
  14. <span title><i nz-icon type="mail"></i> Navigation One</span>
  15. <ul>
  16. <li nz-menu-group>
  17. <span title>Item 1</span>
  18. <ul>
  19. <li nz-menu-item>Option 1</li>
  20. <li nz-menu-item>Option 2</li>
  21. </ul>
  22. </li>
  23. <li nz-menu-group>
  24. <span title>Item 2</span>
  25. <ul>
  26. <li nz-menu-item>Option 3</li>
  27. <li nz-menu-item>Option 4</li>
  28. </ul>
  29. </li>
  30. </ul>
  31. </li>
  32. <li nz-submenu>
  33. <span title><i nz-icon type="appstore"></i> Navigation Two</span>
  34. <ul>
  35. <li nz-menu-item>Option 5</li>
  36. <li nz-menu-item>Option 6</li>
  37. <li nz-submenu>
  38. <span title>Submenu</span>
  39. <ul>
  40. <li nz-menu-item>Option 7</li>
  41. <li nz-menu-item>Option 8</li>
  42. </ul>
  43. </li>
  44. </ul>
  45. </li>
  46. <li nz-submenu>
  47. <span title><i nz-icon type="setting"></i> Navigation Three</span>
  48. <ul>
  49. <li nz-menu-item>Option 9</li>
  50. <li nz-menu-item>Option 10</li>
  51. <li nz-menu-item>Option 11</li>
  52. </ul>
  53. </li>
  54. </ul>
  55. `,
  56. styles: []
  57. })
  58. export class NzDemoMenuSwitchModeComponent {
  59. mode = false;
  60. dark = false;
  61. }

Menu导航菜单 - 图8

递归生成菜单

递归生成菜单,需要手动指定 nzPaddingLeft,仅在 inline 模式下有效。

追踪 Angular Issue:https://github.com/angular/angular/issues/14842

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-menu-recursive',
  4. template: `
  5. <ul nz-menu [nzMode]="'inline'" style="width: 240px;">
  6. <ng-container *ngTemplateOutlet="menuTpl; context: { $implicit: menus }"></ng-container>
  7. <ng-template #menuTpl let-menus>
  8. <ng-container *ngFor="let menu of menus">
  9. <li
  10. nz-menu-item
  11. [nzPaddingLeft]="menu.level * 24"
  12. *ngIf="!menu.children"
  13. [nzDisabled]="menu.disabled"
  14. [nzSelected]="menu.selected"
  15. >
  16. <span title>
  17. <i nz-icon [type]="menu.icon" *ngIf="menu.icon"></i>
  18. <span>{{ menu.title }}</span>
  19. </span>
  20. </li>
  21. <li
  22. nz-submenu
  23. [nzPaddingLeft]="menu.level * 24"
  24. *ngIf="menu.children"
  25. [nzOpen]="menu.open"
  26. [nzDisabled]="menu.disabled"
  27. >
  28. <span title>
  29. <i nz-icon [type]="menu.icon" *ngIf="menu.icon"></i>
  30. <span>{{ menu.title }}</span>
  31. </span>
  32. <ul>
  33. <ng-container *ngTemplateOutlet="menuTpl; context: { $implicit: menu.children }"></ng-container>
  34. </ul>
  35. </li>
  36. </ng-container>
  37. </ng-template>
  38. </ul>
  39. `,
  40. styles: []
  41. })
  42. export class NzDemoMenuRecursiveComponent {
  43. mode = false;
  44. dark = false;
  45. menus = [
  46. {
  47. level: 1,
  48. title: 'Mail Group',
  49. icon: 'mail',
  50. open: true,
  51. selected: false,
  52. disabled: false,
  53. children: [
  54. {
  55. level: 2,
  56. title: 'Group 1',
  57. icon: 'bars',
  58. open: false,
  59. selected: false,
  60. disabled: false,
  61. children: [
  62. {
  63. level: 3,
  64. title: 'Option 1',
  65. selected: false,
  66. disabled: false
  67. },
  68. {
  69. level: 3,
  70. title: 'Option 2',
  71. selected: false,
  72. disabled: true
  73. }
  74. ]
  75. },
  76. {
  77. level: 2,
  78. title: 'Group 2',
  79. icon: 'bars',
  80. selected: true,
  81. disabled: false
  82. },
  83. {
  84. level: 2,
  85. title: 'Group 3',
  86. icon: 'bars',
  87. selected: false,
  88. disabled: false
  89. }
  90. ]
  91. },
  92. {
  93. level: 1,
  94. title: 'Team Group',
  95. icon: 'team',
  96. open: false,
  97. selected: false,
  98. disabled: false,
  99. children: [
  100. {
  101. level: 2,
  102. title: 'User 1',
  103. icon: 'user',
  104. selected: false,
  105. disabled: false
  106. },
  107. {
  108. level: 2,
  109. title: 'User 2',
  110. icon: 'user',
  111. selected: false,
  112. disabled: false
  113. }
  114. ]
  115. }
  116. ];
  117. }

API

  1. <ul nz-menu>
  2. <li nz-menu-item>菜单项</li>
  3. <li nz-submenu>
  4. <span title>子菜单</span>
  5. <ul>
  6. <li nz-menu-item>子菜单项</li>
  7. </ul>
  8. </li>
  9. </ul>

单独引入此组件

想要了解更多关于单独引入组件的内容,可以在快速上手页面进行查看。

  1. import { NzMenuModule } from 'ng-zorro-antd';

[nz-menu]directive

参数说明类型默认值
[nzInlineCollapsed]inline 时菜单是否收起状态boolean-
[nzInlineIndent]inline 模式的菜单缩进宽度number24
[nzMode]菜单类型,现在支持垂直、水平、和内嵌模式三种'vertical'|'horizontal'|'inline''vertical'
[nzSelectable]是否允许选中booleantrue
[nzTheme]主题颜色'light'|'dark''light'
(nzClick)点击 nz-menu-item 输出属性EventEmitter<NzMenuItemDirective>

[nz-menu-item]directive

参数说明类型默认值
[nzDisabled]是否禁用booleanfalse
[nzSelected]是否被选中booleanfalse

[nz-submenu]directive

使用 title 标识符来标定子菜单标题部分

参数说明类型默认值
[nzOpen]是否展开,可双向绑定booleanfalse
[nzDisabled]是否禁用booleanfalse
[nzMenuClassName]自定义子菜单容器类名string-
(nzOpenChange)展开回调EventEmitter<boolean>-

[nz-menu-group]directive

使用 title 标识符来标定标题部分

[nz-menu-divider]directive

菜单项分割线,只用在弹出菜单内。