本示例是ionic3.3,用了懶加載
在
app.html
中疮绷,添加#myNav
钝计,在app.component.ts
文件通過@ViewChild('myNav')
獲茸亍:
<ion-nav #myNav [root]="rootPage"></ion-nav>
在app.component.ts中:獲取
myNav
箍铲,注冊返回事件監(jiān)聽
export class MyApp {
rootPage: any;
backButtonPressed: boolean = false;
@ViewChild('myNav') nav: Nav;
constructor(public platform: Platform,
statusBar: StatusBar,
splashScreen: SplashScreen,
public ionicApp: IonicApp,
public toastCtrl: ToastController,
public util: UtilService,
public keyboard: Keyboard,
public sqlHelp: SqlService) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
this.init();
this.sqlHelp.createTable();
this.registerBackButtonAction();
});
}
}
- 在
tabs.html
中冻押,添加#mainTabs
,在tabs.ts
文件通過@ViewChild('mainTabs') tabs:Tabs
獲取:
<ion-tabs #mainTabs>
<ion-tab [root]="tab1Root" tabUrlPath="work" tabTitle="業(yè)務(wù)" tabIcon="apps"></ion-tab>
<ion-tab [root]="tab2Root" tabUrlPath="report" tabTitle="報表" tabIcon="list-box"></ion-tab>
<ion-tab [root]="tab3Root" tabUrlPath="alarm" tabTitle="提醒" [tabBadge]="tabBadgeNum"
tabIcon="alarm"></ion-tab>
<ion-tab [root]="tab4Root" tabUrlPath="setting" tabTitle="設(shè)置" tabIcon="settings"></ion-tab>
</ion-tabs>
- 在
tabs.ts
中:
import {Component, ViewChild} from '@angular/core';
import {IonicPage,Tabs} from "ionic-angular";
import {UtilService} from '../../providers/util-service';
@IonicPage()
@Component({
templateUrl: 'tabs.html'
})
export class TabsPage {
tabBadgeNum:string;
// this tells the tabs component which Pages
// should be each tab's root Page
@ViewChild('mainTabs') tabs: Tabs;
tab1Root: any = 'WorkPage';
tab2Root: any = 'ReportPage';
tab3Root: any = 'AlarmPage';
tab4Root: any = 'SettingPage';
constructor( private utilService: UtilService) {
}
}
- 在
app.component.ts
寫事件:
registerBackButtonAction() {
this.platform.registerBackButtonAction(() => {
if (this.keyboard.isOpen()) {
//按下返回鍵時鳞陨,先關(guān)閉鍵盤
this.keyboard.close();
return;
};
//如果想點擊返回按鈕隱藏toast或loading或Overlay就把下面加上
// this.ionicApp._toastPortal.gaetActive() || this.ionicApp._loadingPortal.getActive() || this.ionicApp._overlayPortal.getActive()
//不寫this.ionicApp._toastPortal.getActive()是因為連按2次退出
let activePortal = this.ionicApp._modalPortal.getActive() ||this.ionicApp._overlayPortal.getActive();
let loadingPortal = this.ionicApp._loadingPortal.getActive();
if (activePortal) {
//其他的關(guān)閉
activePortal.dismiss().catch(() => {
});
activePortal.onDidDismiss(() => {
});
return;
}
if (loadingPortal) {
//loading的話昨寞,返回鍵無效
return;
}
let activeVC = this.nav.getActive();
let page = activeVC.instance;
if (page instanceof LoginPage || page instanceof WelcomePage) {
this.platform.exitApp();
return;
}
let tabs = activeVC.instance.tabs;
let activeNav = tabs.getSelected();
return activeNav.canGoBack() ? activeNav.pop() : this.showExit();//另外兩種方法在這里將this.showExit()改為其他兩種的方法的邏輯就好。
}, 1);
}
//雙擊退出提示框
showExit() {
if (this.backButtonPressed) { //當觸發(fā)標志為true時厦滤,即2秒內(nèi)雙擊返回按鍵則退出APP
this.platform.exitApp();
} else {
this.toastCtrl.create({
message: '再按一次退出應(yīng)用',
duration: 2000,
position: 'bottom',
cssClass: 'text-align: center'
}).present();
this.backButtonPressed = true;
setTimeout(() => this.backButtonPressed = false, 2000);//2秒內(nèi)沒有再次點擊返回則將觸發(fā)標志標記為false
}
}