OGeek|极客世界-中国程序员成长平台

标题: android - 在 Ionic 4 中处理后退按钮的问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 05:36
标题: android - 在 Ionic 4 中处理后退按钮的问题

当我在 android 上构建并运行我的应用程序时,我意识到硬件后退按钮不起作用。 我有主页,它有两个 child ,一个主页和一个收藏页面,主页是默认页面。 如果我在收藏夹页面并单击后退按钮,它会将我带到主页,但从主页上,如果我单击后退按钮,它会显示白屏并在几秒钟后重新启动应用程序。 如果我单击它三下,我将只能关闭该应用程序。 我正在使用 ionic 4,Angular 8。

这是我的应用程序路由的样子:

  { path: '', loadChildren: './pages/main/main.module#MainPageModule' },
  { path: 'search', loadChildren: 
  './pages/search/search.module#SearchPageModule' },
  { path: 'catalog-view', loadChildren: './pages/cat-view/cat- 
  view.module#CatViewPageModule' },

这是我的主模块的样子:

const routes: Routes = [
  {
    path: 'main',
    component: MainPage,
    children: [
     { path: 'home', loadChildren: '../home/home.module#HomePageModule' },
      { path: 'favorites', loadChildren: 
     '../favorites/favorites.module#FavoritesPageModule' },
       ]
      },
      {
        path: '',
       redirectTo: '/main/home'
       }
   ];

我尝试在主页中添加此功能,然后是主页面,甚至是 App 组件,但没有任何效果:

ionViewDidEnter(){
  this.subscription = this.platform.backButton.subscribe(()=>{
      navigator['app'].exitApp();
  });
}

 ionViewWillLeave(){
  this.subscription.unsubscribe();
 }



Best Answer-推荐答案


我得到了它的工作,这个问题非常愚蠢。 我不得不将我的包更新到最新版本,尤其是@ionic-native/core。 在我的 home.ts 中,我只是这样做:

ngOnInit(){
     this.platform.backButton.subscribe(() => {
     this.platform.backButton.unsubscribe();
     navigator['app'].exitApp();
   })
)

希望对你有帮助!

关于android - 在 Ionic 4 中处理后退按钮的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57515194/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4