【问题标题】:How force Ionic 4 back button to always goto a certain Page from selected pages如何强制 Ionic 4 后退按钮始终从选定页面转到某个页面
【发布时间】:2021-12-07 08:01:03
【问题描述】:

我正在将我的 Ionic 3 应用程序移植到 Ionic 4。我已切换到使用新路由,以及延迟加载模块等。

我有一个侧面菜单,我可以在其中从任何其他页面中选择我的主页。

但是,每当我从一个主要功能页面单击返回按钮时,我总是想返回主主页(而不是返回当前路由堆栈中的另一个功能页面)

Ionic 4 版本中侧边菜单选择的点击处理代码如下(pageItem.pageRoutePath 包含新路由)

      public openPage(pageItem: AppPageItem): void {
        try {
          this.logger.debug(`openPage ${pageItem.title}`);
          
          // Try to force homepage to be where the back button takes us (but this doesn't seem to work)
          this.navController.navigateRoot(Constants.vals.pageRoutePaths.home, { replaceUrl: true });
          if (pageItem.pageRoutePath != Constants.vals.pageRoutePaths.home) {
            this.navController.navigateForward(pageItem.pageRoutePath);
          }
        } catch (error) {
          this.logger.error(`openPage ${error}`);
        }
      }

{ replaceUrl: true } 参数是我尝试让它工作,但它没有任何区别。

在 Ionic 3 中,我可以做我所描述的如下...

this.nav.setRoot(HomePage);
  if (pageItem.page != HomePage) {
    this.nav.push(pageItem.page); 
  }  

我不希望从每个页面中获取此内容,只希望在我的功能页面中,例如一些功能页面导航到“详细信息:页面,我确实想要后退按钮只是返回到它导航的页面,即默认行为。

如何在 Ionic 4 中实现这一点?

19 年 4 月 28 日更新

我以为我可以使用..

 this.navController.navigateForward(pageItem.pageRoutePath, { replaceUrl: true });

如果只有一个额外的页面,它可以工作,但如果有一个完整的堆栈,则不能 - 所以问题仍然存在(因此将其作为答案删除)。

【问题讨论】:

    标签: angular ionic-framework


    【解决方案1】:

    我们可以使用路由器来做到这一点:

    let navigationExtras: NavigationExtras = {
    skipLocationChange: true,
    replaceUrl: true
     };
    
    
    //Where page is the page where app will navigate
    this.router.navigate([’/page’], navigationExtras);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多