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

无动画

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

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

固定高度

固定外部容器高度

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

动态创建

TabPane被动态创建

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

垂直样式

tabDirection为vertical

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

TabTitle固定尺寸

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

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-tabs-fixedtabtitlesize',
  4. template: `
  5. <Tabs
  6. [page]="3"
  7. [useOnPan]="true"
  8. [swipeable]="true"
  9. [activeTab]="index"
  10. [tabTitleSize]="100"
  11. [tabBarActiveTextColor]="'#1890ff'"
  12. (onChange)="onChange($event)"
  13. (onTabClick)="onTabClick($event)"
  14. >
  15. <TabPane [title]="titleTemplate">
  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. <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. </Tabs>
  36. <WhiteSpace></WhiteSpace>
  37. <Tabs
  38. style="height: 200px;"
  39. [page]="3"
  40. [activeTab]="index"
  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
  49. style="display: flex; height: 200px; width: 100%; background-color: white;align-items: center;justify-content: center;"
  50. >
  51. Content of first tab
  52. </div>
  53. </TabPane>
  54. <TabPane [title]="'Second Tab'">
  55. <div
  56. style="display: flex; height: 200px; width: 100%; background-color: white;align-items: center;justify-content: center;"
  57. >
  58. Content of second tab
  59. </div>
  60. </TabPane>
  61. <TabPane [title]="'Third Tab'">
  62. <div
  63. style="display: flex; height: 200px; width: 100%; background-color: white;align-items: center;justify-content: center;"
  64. >
  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 DemoTabsFixedtabtitlesizeComponent {
  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. }

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

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

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

Tabs标签页 - 图1

API

Tabs

参数说明类型默认值
[activeTab]当前激活Tab索引number0
[tabBarPosition]TabBar位置‘top’ | ‘bottom’ | ‘left’ | ‘right’‘top’
[page]Tab分页尺寸number5
[swipeable]是否可以滑动内容切换booleantrue
[useOnPan]使用跟手滚动booleantrue
[animated]是否开启切换动画booleantrue
[distanceToChangeTab]滑动切换阈值(宽度比例)number0.3
[prerenderingSiblingsNumber]预加载两侧Tab数量, -1: 加载所有的Tab内容, 0: 仅加载当前tab内容, n: 预加载两侧n个Tabnumber-1
[tabDirection]Tab方向‘horizontal’ | ‘vertical’‘horizontal’
[tabBarUnderlineStyle]TabBar下划线样式object-
[tabBarBackgroundColor]TabBar背景色string-
[tabBarActiveTextColor]TabBar激活Tab文字颜色string-
[tabBarInactiveTextColor]TabBar非激活Tab文字颜色string-
[tabBarTextStyle]TabBar文字样式object-
[renderTabBar]替换TabBar的TabTemplateRef-
(onChange)Tab变化时触发EventEmitter<{index: number}>-
(onTabClick)Tab 被点击的回调EventEmitter<{index: number}>-

TabPane

参数说明类型默认值
[title]tab面板的标题string | TemplateRef-