Tabs标签页

用于让用户在不同的视图中进行切换。

规则

  • 标签数量,一般 2-4 个;其中,标签中的文案需要精简,一般 2-4 个字。
  • 在 iOS 端的次级页面中,不建议使用左右滑动来切换 Tab,这个和 iOS 的左滑返回存在冲突,eg:详情页中 Tabs。

代码演示

基本用法

最简单的用法。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-tabs-basic',
  4. template: `
  5. <Tabs [useOnPan]="true"
  6. [swipeable]="true"
  7. [activeTab]="index"
  8. [page]="3"
  9. [tabBarActiveTextColor]="'#1890ff'"
  10. (onChange)="onChange($event)"
  11. (onTabClick)="onTabClick($event)"
  12. >
  13. <TabPane [title]="titleTemplate"
  14. >
  15. <ng-template #titleTemplate>
  16. <Badge [text]="3">
  17. <div>First Tab</div>
  18. </Badge>
  19. </ng-template>
  20. <div style="display: flex; align-items: center;justify-content: center;height: 150px;background-color: #fff">
  21. Content of first tab
  22. </div>
  23. </TabPane>
  24. <TabPane [title]="titleTemplate1"
  25. >
  26. <ng-template #titleTemplate1>
  27. <Badge [text]="'今日(20)'">
  28. <div>Second Tab</div>
  29. </Badge>
  30. </ng-template>
  31. <div style="display: flex; align-items: center;justify-content: center;height: 150px;background-color: #fff">
  32. Content of second tab
  33. </div>
  34. </TabPane>
  35. <TabPane [title]="titleTemplate2">
  36. <ng-template #titleTemplate2>
  37. <Badge [dot]="true">
  38. <div>Third Tab</div>
  39. </Badge>
  40. </ng-template>
  41. <div style="display: flex; align-items: center;justify-content: center;height: 150px;background-color: #fff">
  42. Content of third tab
  43. </div>
  44. </TabPane>
  45. </Tabs>
  46. <WhiteSpace></WhiteSpace>
  47. <Tabs [activeTab]="index"
  48. [page]="3"
  49. [tabBarPosition]="'bottom'"
  50. (onChange)="onChange($event)"
  51. (onTabClick)="onTabClick($event)"
  52. >
  53. <TabPane [title]="'First Tab'">
  54. <div style="display: flex; height: 150px; width: 100%; background-color: white;align-items: center;justify-content: center;">
  55. Content of first tab
  56. </div>
  57. </TabPane>
  58. <TabPane [title]="'Second Tab'">
  59. <div style="display: flex; height: 150px; width: 100%; background-color: white;align-items: center;justify-content: center;">
  60. Content of second tab
  61. </div>
  62. </TabPane>
  63. <TabPane [title]="'Third Tab'">
  64. <div style="display: flex; height: 150px; width: 100%; background-color: white;align-items: center;justify-content: center;">
  65. Content of third tab
  66. </div>
  67. </TabPane>
  68. </Tabs>
  69. `,
  70. styles: [
  71. `
  72. /deep/ .am-badge {
  73. text-align: right;
  74. }
  75. `
  76. ]
  77. })
  78. export class DemoTabsBasicComponent {
  79. flag = true;
  80. index = 1;
  81. onChange(item) {
  82. console.log('onChange', item);
  83. }
  84. onTabClick(item) {
  85. console.log('onTabClick', item);
  86. }
  87. selectCard(e) {
  88. console.log(' ', JSON.stringify(e));
  89. }
  90. changeIndex() {
  91. this.index = 0;
  92. }
  93. }

无动画

切换Tab标签页不带动画效果

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-tabs-noanim',
  4. template: `
  5. <Tabs [activeTab]="index"
  6. [page]="3"
  7. [useOnPan]="false"
  8. [animated]="false"
  9. (onChange)="onChange($event)"
  10. (onTabClick)="onTabClick($event)"
  11. >
  12. <TabPane [title]="'First Tab'">
  13. <div style="display: flex; height: 150px; width: 100%; background-color: white;align-items: center;justify-content: center;">
  14. Content of first tab
  15. </div>
  16. </TabPane>
  17. <TabPane [title]="'Second Tab'">
  18. <div style="display: flex; height: 150px; width: 100%; background-color: white;align-items: center;justify-content: center;">
  19. Content of second tab
  20. </div>
  21. </TabPane>
  22. <TabPane [title]="'Third Tab'">
  23. <div style="display: flex; height: 150px; width: 100%; background-color: white;align-items: center;justify-content: center;">
  24. Content of third tab
  25. </div>
  26. </TabPane>
  27. </Tabs>
  28. `,
  29. styles: [
  30. `
  31. /deep/ .am-badge {
  32. text-align: right;
  33. }
  34. `
  35. ]
  36. })
  37. export class DemoTabsNoanimComponent {
  38. index = 0;
  39. onChange(item) {
  40. console.log('onChange', item);
  41. }
  42. onTabClick(item) {
  43. console.log('onTabClick', item);
  44. }
  45. }

固定高度

固定外部容器高度

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-tabs-fixedheight',
  4. template: `
  5. <Tabs style="height: 200px;"
  6. [activeTab]="index"
  7. [page]="3"
  8. (onChange)="onChange($event)"
  9. (onTabClick)="onTabClick($event)"
  10. >
  11. <TabPane [title]="'First Tab'">
  12. <div style="display: flex; align-items: center; justify-content: center; height: 250px; background-color: rgb(255, 255, 255);">
  13. Content of first tab
  14. </div>
  15. </TabPane>
  16. <TabPane [title]="'Second Tab'">
  17. <div style="display: flex; align-items: center; justify-content: center; height: 250px; background-color: rgb(255, 255, 255);">
  18. Content of second tab
  19. </div>
  20. </TabPane>
  21. <TabPane [title]="'Third Tab'">
  22. <div style="display: flex; align-items: center; justify-content: center; height: 250px; background-color: rgb(255, 255, 255);">
  23. Content of third tab
  24. </div>
  25. </TabPane>
  26. </Tabs>
  27. `
  28. })
  29. export class DemoTabsFixedheightComponent {
  30. index = 0;
  31. onChange(item) {
  32. console.log('onChange', item);
  33. }
  34. onTabClick(item) {
  35. console.log('onTabClick', item);
  36. }
  37. }

动态创建

TabPane被动态创建

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-tabs-dynamic',
  4. template: `
  5. <Tabs [useOnPan]="true"
  6. [swipeable]="true"
  7. [activeTab]="activeTabIndex"
  8. [page]="5"
  9. [tabBarActiveTextColor]="'#1890ff'"
  10. (onChange)="onChange($event)"
  11. (onTabClick)="onTabClick($event)"
  12. >
  13. <TabPane *ngFor="let tabListItem of tabList; let i = index"
  14. [title]=tabListItem.title
  15. >
  16. <div style="display: block; padding: 40px; text-align: center; align-items: center;justify-content: center;height: 150px;background-color: #fff">
  17. <div>{{tabListItem.content}}</div>
  18. <div Button [type]="'primary'" (onClick)="onClick()">Add Pane + </div>
  19. </div>
  20. </TabPane>
  21. </Tabs>
  22. `,
  23. styles: [
  24. `
  25. /deep/ .am-badge {
  26. text-align: right;
  27. }
  28. `
  29. ]
  30. })
  31. export class DemoTabsDynamicComponent {
  32. flag = true;
  33. activeTabIndex = 0;
  34. tabList: any[] = [{
  35. title: '1st Tab',
  36. content: '1st Tab Content'
  37. }, {
  38. title: '2nd Tab',
  39. content: '2nd Tab Content'
  40. }, {
  41. title: '3rd Tab',
  42. content: '3rd Tab Content'
  43. }];
  44. onChange(item) {
  45. console.log('onChange', item);
  46. }
  47. onTabClick(item) {
  48. console.log('onTabClick', item);
  49. }
  50. selectCard(e) {
  51. console.log(' ', JSON.stringify(e));
  52. }
  53. changeIndex() {
  54. this.activeTabIndex = 0;
  55. }
  56. onClick() {
  57. this.tabList.push({
  58. title: '' + (this.tabList.length + 1) + 'th Tab',
  59. content: '' + (this.tabList.length + 1) + 'th Tab Content'
  60. });
  61. }
  62. }

垂直样式

tabDirection为vertical

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-tabs-vertical',
  4. template: `
  5. <Tabs style="height: 200px;"
  6. [activeTab]="index"
  7. [page]="3"
  8. [tabBarPosition]="'left'"
  9. [tabDirection]="'vertical'"
  10. (onChange)="onChange($event)"
  11. (onTabClick)="onTabClick($event)"
  12. >
  13. <TabPane [title]="'First Tab'">
  14. <div style="display: flex; height: 200px; width: 100%; background-color: white;align-items: center;justify-content: center;">
  15. Content of first tab
  16. </div>
  17. </TabPane>
  18. <TabPane [title]="'Second Tab'">
  19. <div style="display: flex; height: 200px; width: 100%; background-color: white;align-items: center;justify-content: center;">
  20. Content of second tab
  21. </div>
  22. </TabPane>
  23. <TabPane [title]="'Third Tab'">
  24. <div style="display: flex; height: 200px; width: 100%; background-color: white;align-items: center;justify-content: center;">
  25. Content of third tab
  26. </div>
  27. </TabPane>
  28. </Tabs>
  29. `,
  30. styles: [
  31. `
  32. /deep/ .am-badge {
  33. text-align: right;
  34. }
  35. `
  36. ]
  37. })
  38. export class DemoTabsVerticalComponent {
  39. index = 0;
  40. onChange(item) {
  41. console.log('onChange', item);
  42. }
  43. onTabClick(item) {
  44. console.log('onTabClick', item);
  45. }
  46. }

TabTitle固定尺寸

为TabTitle设置尺寸(单位:px)。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-tabs-fixedtabtitlesize',
  4. template: `
  5. <Tabs [useOnPan]="true"
  6. [swipeable]="true"
  7. [activeTab]="index"
  8. [page]="3"
  9. [tabTitleSize]="100"
  10. [tabBarActiveTextColor]="'#1890ff'"
  11. (onChange)="onChange($event)"
  12. (onTabClick)="onTabClick($event)"
  13. >
  14. <TabPane [title]="titleTemplate"
  15. >
  16. <ng-template #titleTemplate>
  17. <Badge [text]="3">
  18. <div>First Tab</div>
  19. </Badge>
  20. </ng-template>
  21. <div style="display: flex; align-items: center;justify-content: center;height: 150px;background-color: #fff">
  22. Content of first tab
  23. </div>
  24. </TabPane>
  25. <TabPane [title]="titleTemplate1"
  26. >
  27. <ng-template #titleTemplate1>
  28. <Badge [text]="'今日(20)'">
  29. <div>Second Tab</div>
  30. </Badge>
  31. </ng-template>
  32. <div style="display: flex; align-items: center;justify-content: center;height: 150px;background-color: #fff">
  33. Content of second tab
  34. </div>
  35. </TabPane>
  36. </Tabs>
  37. <WhiteSpace></WhiteSpace>
  38. <Tabs style="height: 200px;"
  39. [activeTab]="index"
  40. [page]="3"
  41. [tabTitleSize]="40"
  42. [tabBarPosition]="'left'"
  43. [tabDirection]="'vertical'"
  44. (onChange)="onChange($event)"
  45. (onTabClick)="onTabClick($event)"
  46. >
  47. <TabPane [title]="'First Tab'">
  48. <div style="display: flex; height: 200px; width: 100%; background-color: white;align-items: center;justify-content: center;">
  49. Content of first tab
  50. </div>
  51. </TabPane>
  52. <TabPane [title]="'Second Tab'">
  53. <div style="display: flex; height: 200px; width: 100%; background-color: white;align-items: center;justify-content: center;">
  54. Content of second tab
  55. </div>
  56. </TabPane>
  57. <TabPane [title]="'Third Tab'">
  58. <div style="display: flex; height: 200px; width: 100%; background-color: white;align-items: center;justify-content: center;">
  59. Content of third tab
  60. </div>
  61. </TabPane>
  62. </Tabs>
  63. `,
  64. styles: [
  65. `
  66. /deep/ .am-badge {
  67. text-align: right;
  68. }
  69. `
  70. ]
  71. })
  72. export class DemoTabsFixedtabtitlesizeComponent {
  73. flag = true;
  74. index = 1;
  75. onChange(item) {
  76. console.log('onChange', item);
  77. }
  78. onTabClick(item) {
  79. console.log('onTabClick', item);
  80. }
  81. selectCard(e) {
  82. console.log(' ', JSON.stringify(e));
  83. }
  84. changeIndex() {
  85. this.index = 0;
  86. }
  87. }

自定义个数,超出界面宽度,多于5个标签

界面可见区域最多存在5个tab标签页,更多内容可以左右滑动标签页

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-tabs-multitabs',
  4. template: `
  5. <Tabs [activeTab]="index"
  6. [page]="3"
  7. [prerenderingSiblingsNumber]="2"
  8. (onChange)="selectCard($event)"
  9. (onTabClick)="selectCard($event)"
  10. >
  11. <TabPane [title]="'1st Tab'">
  12. <div style="display: flex; align-items: center;justify-content: center;height: 150px;background-color: #fff">
  13. Content of 1st tab
  14. </div>
  15. </TabPane>
  16. <TabPane [title]="'2nd Tab'">
  17. <div style="display: flex; align-items: center;justify-content: center;height: 150px;background-color: #fff">
  18. Content of 2nd tab
  19. </div>
  20. </TabPane>
  21. <TabPane [title]="'3rd Tab'">
  22. <div style="display: flex; align-items: center;justify-content: center;height: 150px;background-color: #fff">
  23. Content of 3rd tab
  24. </div>
  25. </TabPane>
  26. <TabPane [title]="'4th Tab'">
  27. <div style="display: flex; align-items: center;justify-content: center;height: 150px;background-color: #fff">
  28. Content of 4th tab
  29. </div>
  30. </TabPane>
  31. <TabPane [title]="'5th Tab'">
  32. <div style="display: flex; align-items: center;justify-content: center;height: 150px;background-color: #fff">
  33. Content of 5th tab
  34. </div>
  35. </TabPane>
  36. <TabPane [title]="'6th Tab'">
  37. <div style="display: flex; align-items: center;justify-content: center;height: 150px;background-color: #fff">
  38. Content of 6th tab
  39. </div>
  40. </TabPane>
  41. <TabPane [title]="'7th Tab'">
  42. <div style="display: flex; align-items: center;justify-content: center;height: 150px;background-color: #fff">
  43. Content of 7th tab
  44. </div>
  45. </TabPane>
  46. <TabPane [title]="'8th Tab'">
  47. <div style="display: flex; align-items: center;justify-content: center;height: 150px;background-color: #fff">
  48. Content of 8th tab
  49. </div>
  50. </TabPane>
  51. <TabPane [title]="'9th Tab'">
  52. <div style="display: flex; align-items: center;justify-content: center;height: 150px;background-color: #fff">
  53. Content of 9th tab
  54. </div>
  55. </TabPane>
  56. </Tabs>
  57. `,
  58. styles: [
  59. `
  60. /deep/ .am-badge {
  61. text-align: right;
  62. }
  63. `
  64. ]
  65. })
  66. export class DemoTabsMultitabsComponent {
  67. index = 0;
  68. onChange(item) {
  69. console.log('onChange', item);
  70. }
  71. onTabClick(item) {
  72. console.log('onTabClick', item);
  73. }
  74. selectCard(e) {
  75. console.log(' ', JSON.stringify(e));
  76. }
  77. }

Tabs 标签页 - 图1

API

Tabs

属性说明类型默认值必选
activeTab当前激活Tab索引number0true
tabBarPositionTabBar位置'top' | 'bottom' | 'left' | 'right'topfalse
pageTab分页尺寸number5false
swipeable是否可以滑动内容切换booleantruefalse
useOnPan使用跟手滚动booleantruefalse
animated是否开启切换动画booleantruefalse
onChangetab变化时触发(index: number) => voidfalse
onTabClicktab 被点击的回调(index: number) => voidfalse
distanceToChangeTab滑动切换阈值(宽度比例)number0.3false
prerenderingSiblingsNumber预加载两侧Tab数量, -1: 加载所有的tab内容, 0: 仅加载当前tab内容, n: 预加载两侧n个Tabnumber-1false
tabDirectionTab方向'horizontal' | 'vertical'horizontalfalse
tabBarUnderlineStyletabBar下划线样式objectfalse
tabBarBackgroundColortabBar背景色stringfalse
tabBarActiveTextColortabBar激活Tab文字颜色stringfalse
tabBarInactiveTextColortabBar非激活Tab文字颜色stringfalse
tabBarTextStyletabBar文字样式objectfalse
renderTabBar替换TabBar的TabTemplateRefnullfalse

TabPane

属性说明类型默认值必选
titletab面板的标题string | TemplateReftrue