使用 Ionic v4,我无法从选项卡控制器获取索引,但我通过保留选项卡的列表对象并以这种方式获取数字索引来解决此问题。
package.json
"@ionic/angular": "^4.9.1"
"@angular/core": "^8.2.7"
"com.telerik.plugins.nativepagetransitions": "^0.6.5",
Tabs.ts:
import { Component, OnInit, ViewChild } from '@angular/core';
import { IonTabs } from '@ionic/angular';
import {
NativePageTransitions,
NativeTransitionOptions,
} from '@ionic-native/native-page-transitions/ngx';
@Component({
selector: 'app-tabs',
templateUrl: 'tabs.page.html',
styleUrls: ['tabs.page.scss']
})
export class TabsPage implements OnInit {
@ViewChild('navTabs', { static: false }) navTabs: IonTabs;
private tabs = {
events: 0,
notifications: 1,
profile: 2
};
loaded = false;
tabIndex = 0;
constructor(
private nativePageTransitions: NativePageTransitions
) { }
private getAnimationDirection(e: any): string {
const currentIndex = this.tabIndex;
this.tabIndex = this.tabs[e.tab];
switch (true) {
case currentIndex > this.tabs[e.tab]:
return 'left';
case currentIndex < this.tabs[e.tab]:
return 'right';
}
}
transition(e: any): void {
const options: NativeTransitionOptions = {
direction: this.getAnimationDirection(e),
duration: 250,
slowdownfactor: -1,
slidePixels: 0,
iosdelay: 20,
androiddelay: 0,
fixedPixelsTop: 0,
fixedPixelsBottom: 48,
};
if (!this.loaded) {
this.loaded = true;
return;
}
this.nativePageTransitions.slide(options);
}
}
Tabs.html
<ion-tabs #navTabs (ionTabsWillChange)="transition($event)">
<ion-tab-bar color="dark" slot="bottom">
<ion-tab-button tab="events">
<ion-icon name="calendar"></ion-icon>
<ion-label>Events</ion-label>
</ion-tab-button>
<ion-tab-button tab="notifications">
<ion-icon name="notifications"></ion-icon>
<ion-label>Notification</ion-label>
</ion-tab-button>
<ion-tab-button tab="profile">
<ion-icon name="barcode"></ion-icon>
<ion-label>Profile</ion-label>
</ion-tab-button>
</ion-tab-bar>
</ion-tabs>