ionic-framework - ionicV3 后退按钮导航问题显示 nav.getActive 不是函数
问题描述
我正在尝试使用 ionic 3 将导航返回按钮覆盖到我在 android 中的需要,但是每当我运行我的应用程序时,当我按下返回导航按钮说“nav.getActive
不是一个功能”时,它会显示一个错误,因为这个返回按钮不起作用。
更新的代码如下
export class MyApp {
rootPage:any = LoginPage;
public counter=0;
@ViewChild(Nav) nav: Nav;
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,public globalProvider: GlobalProvider,
public googlePlus: GooglePlus, public zone: NgZone, private readonly firebase: Firebase, private alertCtrl: AlertController,
public toastCtrl: ToastController, public app: App) {
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();
platform.registerBackButtonAction(() => {
const activeView = this.nav.getActive().name // This will give you the activeview name
if(activeView === 'HomePage') {
if (this.nav.canGoBack()){ //Can we go back?
this.nav.pop();
} else {
const alert = this.alertCtrl.create({
title: 'App termination',
message: 'Do you want to close the app?',
buttons: [{
text: 'Cancel',
role: 'cancel',
handler: () => {
console.log('Application exit prevented!');
}
},{
text: 'Close App',
handler: () => {
platform.exitApp(); // Close this application
}
}]
});
alert.present();
}
}
this.nav.push(TabsPage);
}, 1);
this.firebase.onNotificationOpen().subscribe(notification => {
console.log('notification info: ', notification);
/*!notification.tap
? console.log('The user was using the app when the notification arrived...')
: console.log('The app was closed when the notification arrived...');
let notificationAlert = this.alertCtrl.create({
title: notification.title,
message: notification.body,
buttons: ['Ok']
});
notificationAlert.present();*/
},
error => {
console.error('Error getting the notification', error);
});
});
/*platform.registerBackButtonAction(() => {
if (this.counter == 0) {
this.counter++;
this.presentToast();
setTimeout(() => { this.counter = 0 }, 3000)
} else {
// console.log("exitapp");
platform.exitApp();
}
}, 0) });*/
}
解决方案
像这样试试。
@ViewChild(Nav) nav: Nav;
constructor(platform: Platform,
statusBar: StatusBar,
splashScreen: SplashScreen,
public globalProvider: GlobalProvider,
public toastCtrl: ToastController,
public googlePlus: GooglePlus,
public zone: NgZone,
public app: App,
public alertCtrl: AlertController
) {
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();
platform.registerBackButtonAction(() => {
**const activeView = this.nav.getActive().name** // This will give you the activeview name
if(active.instance instanceof HomePage) {
if (nav.canGoBack()){ //Can we go back?
nav.pop();
} else {
const alert = this.alertCtrl.create({
title: 'App termination',
message: 'Do you want to close the app?',
buttons: [{
text: 'Cancel',
role: 'cancel',
handler: () => {
console.log('Application exit prevented!');
}
},{
text: 'Close App',
handler: () => {
platform.exitApp(); // Close this application
}
}]
});
alert.present();
}
}
nav.push(HomePage);
}, 100);
});
}
推荐阅读
- javascript - 如何使用 chrome 开发工具获取 youtube 成绩单 url?
- xsd - xsd复杂类型继承中的限制问题
- python - 尝试使用 Anaconda-navigator 安装 Tensorflow,但在导航器中找不到包
- flutter - 发射状态更改后保持 UI Cubit Flutter
- python - 合并日期时间和范围的数据框
- php - 在 apache2 启动时启动 php 脚本
- snowflake-cloud-data-platform - Infromatica 云 - 由于以下错误,Snowflake 连接器无法从源读取数据:null
- c# - 当 .csproj 被称为 Nuget 包时出现 FileNotFoundException
- javascript - 使用虚拟主机服务器从网站发送电子邮件
- amazon-fire-tv - 为什么我在 FireTV 4K 上设置代理时卡在登录页面