Tabs标签页

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

基本用法

最简单的用法。

无动画

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

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

固定外部容器高度

动态创建

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. style="display: block; padding: 40px; text-align: center; align-items: center;justify-content: center;height: 150px;background-color: #fff"
  16. >
  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. {
  36. title: '1st Tab',
  37. content: '1st Tab Content'
  38. },
  39. {
  40. title: '2nd Tab',
  41. content: '2nd Tab Content'
  42. },
  43. {
  44. title: '3rd Tab',
  45. content: '3rd Tab Content'
  46. }
  47. ];
  48. onChange(item) {
  49. console.log('onChange', item);
  50. }
  51. onTabClick(item) {
  52. console.log('onTabClick', item);
  53. }
  54. selectCard(e) {
  55. console.log(' ', JSON.stringify(e));
  56. }
  57. changeIndex() {
  58. this.activeTabIndex = 0;
  59. }
  60. onClick() {
  61. this.tabList.push({
  62. title: '' + (this.tabList.length + 1) + 'th Tab',
  63. content: '' + (this.tabList.length + 1) + 'th Tab Content'
  64. });
  65. }
  66. }

垂直样式

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. </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. </Tabs>
  35. <WhiteSpace></WhiteSpace>
  36. <Tabs
  37. style="height: 200px;"
  38. [page]="3"
  39. [activeTab]="index"
  40. [tabTitleSize]="40"
  41. [tabBarPosition]="'left'"
  42. [tabDirection]="'vertical'"
  43. (onChange)="onChange($event)"
  44. (onTabClick)="onTabClick($event)"
  45. >
  46. <TabPane [title]="'First Tab'">
  47. <div
  48. style="display: flex; height: 200px; width: 100%; background-color: white;align-items: center;justify-content: center;"
  49. >
  50. </div>
  51. </TabPane>
  52. <TabPane [title]="'Second Tab'">
  53. <div
  54. style="display: flex; height: 200px; width: 100%; background-color: white;align-items: center;justify-content: center;"
  55. >
  56. Content of second tab
  57. </div>
  58. </TabPane>
  59. <TabPane [title]="'Third Tab'">
  60. <div
  61. style="display: flex; height: 200px; width: 100%; background-color: white;align-items: center;justify-content: center;"
  62. >
  63. Content of third tab
  64. </div>
  65. </TabPane>
  66. </Tabs>
  67. `,
  68. styles: [
  69. `
  70. /deep/ .am-badge {
  71. text-align: right;
  72. }
  73. `
  74. ]
  75. })
  76. export class DemoTabsFixedtabtitlesizeComponent {
  77. flag = true;
  78. index = 1;
  79. onChange(item) {
  80. console.log('onChange', item);
  81. }
  82. onTabClick(item) {
  83. console.log('onTabClick', item);
  84. }
  85. selectCard(e) {
  86. console.log(' ', JSON.stringify(e));
  87. }
  88. changeIndex() {
  89. this.index = 0;
  90. }

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

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

API

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