Ionic framework Ionic 4/5:按后退按钮退出应用程序
我需要退出应用程序时,返回按钮只在某一页按下;特别是,假设我有app.component.ts,我有一个名为HomePage的页面。在app-routing.module.ts中,我有以下路线:Ionic framework Ionic 4/5:按后退按钮退出应用程序,ionic-framework,ionic4,Ionic Framework,Ionic4,我需要退出应用程序时,返回按钮只在某一页按下;特别是,假设我有app.component.ts,我有一个名为HomePage的页面。在app-routing.module.ts中,我有以下路线: const routes: Routes = [ { path: '', redirectTo: 'home', pathMatch: 'full' }, { path: 'home', loadChildren: './public/home/home.module#HomePageModule' }
const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', loadChildren: './public/home/home.module#HomePageModule' }
];
因此,当用户打开应用程序时,它会将主页视为第一页。如果用户按下后退按钮,现在它将永远重新加载同一页面;我想抓住按下按钮并退出应用程序。我尝试在home.page.ts中使用此选项,但没有任何效果:
backButtonSubscription: any;
this.backButtonSubscription = this.platform.backButton.subscribe(async () => {
console.log('lets exit!');
navigator['app'].exitApp();
});
即使是console.log也会打印出来,就好像没有捕获事件一样。我也试过这样做:
this.platform.backButton.subscribeWithPriority(99999, () => {
navigator['app'].exitApp();
});
但是结果是一样的,所以我想知道如何解决它。我在网上发现了很多问题,但似乎没有任何解释可以解决这个问题或提供解决办法
谢谢你的帮助 如果您使用的是离子4角。试试这个 在AppComponent->initializeApp中 加 这里有一个例子
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.platform.backButton.subscribeWithPriority(0, () => {
navigator['app'].exitApp();
});
});
}
在我的例子中,我使用的是电容器(离子5),如果你也使用电容器,试试这个
import { Plugins } from '@capacitor/core';
const { App } = Plugins;
initializeApp() {
App.addListener('backButton', () => {
App.exitApp();
});
}
离子5。
另一个选择是使用平台
,对我来说,在使用标签的情况下,它工作得很好:
p、 在美国,最好使用“init”服务而不是app.component.ts,以保持app init流干净和明显
constructor(
private platform: Platform,
private router: Router
) {
this.platform.backButton.subscribeWithPriority(-1, () => {
const url = this.router.url;
if (url === '/tabs/not-home') {
this.router.navigate(['/tabs/home']);
} else if (url === '/tabs/home') {
App.exitApp();
}
});
}
你能用html发布后退按钮的代码吗?没有html代码,因为它应该捕捉设备的后退按钮
constructor(
private platform: Platform,
private router: Router
) {
this.platform.backButton.subscribeWithPriority(-1, () => {
const url = this.router.url;
if (url === '/tabs/not-home') {
this.router.navigate(['/tabs/home']);
} else if (url === '/tabs/home') {
App.exitApp();
}
});
}