【问题标题】:Ionic 2 Angular NavController, pop back to second last pageIonic 2 Angular NavController,弹回倒数第二页
【发布时间】:2017-02-21 08:04:34
【问题描述】:

我有以下导航案例:

Home -> navCtrl.push(SearchPage) -> navCtrl.push(ResultPage)

Home -> navCtrl.push(SearchPage) -> navCtrl.push(ResultPage) -> navCtrl.push(DetailPage)

我想导航回 SearchPage。在第一种情况下,没有问题,我可以使用

this.navCtrl.pop()

但是,在第二种情况下,我尝试使用

this.navCtrl.popTo(SearchPage)

这并没有按预期工作。 Ionic 只在堆栈中导航一页。我知道 popTo() 有问题 (https://github.com/driftyco/ionic/issues/6513)

我该如何解决这个问题?

【问题讨论】:

    标签: angular ionic2


    【解决方案1】:

    试试这个!进入DetailPage 后,请执行以下操作:

    this.navCtrl.remove(2,1); // This will remove the 'ResultPage' from stack.
    this.navCtrl.pop();  // This will pop the 'DetailPage', and take you to 'SearchPage'
    

    【讨论】:

      【解决方案2】:
      this.navController.push(SearchPage).then(() => {
        const index = this.viewCtrl.index;
        this.navController.remove(index, 1); //this will remove page3 and page2
      });
      

      在您的组件中包含此代码。

      【讨论】:

        【解决方案3】:

        我在这里回答了一个类似的问题,它似乎也适用于你的问题。由于我延迟加载模块,因此 popTo() 似乎仅在我从 NavController.getViews() 数组传递对象时才起作用。 话虽如此,您可以尝试这样的事情:

        let targetView = this._navCtrl.getViews().filter(view=> view.id == 'MyAwesomePage')
        targetView.length ? this._navCtrl.popTo(targetView[0]) : this._navCtrl.pop()
        

        或者像这样更冗长的东西:

        let index: number;
        let views: any[] = this._navCtrl.getViews()
        let found: boolean = views.some((view, i) =>{
          index = i
          return (view.id == 'MyAwesomePage')
        })
        found ? this._navCtrl.popTo(views[index]) : this._navCtrl.pop()
        

        如果你想改变顺序,你可以getViews().reverse().filter()或者views.reverse().some()。这是 Ionic 3 和 ES5 的 Array.some()

        【讨论】:

          【解决方案4】:

          好的,找到了解决方案。看起来它可以工作......至少目前是这样

          this.navCtrl
                  .push(SearchPage)
                  .then(() => {
          
                      const index = this.viewCtrl.index;
          
                      for(let i = index; i > 0; i--){
                          this.navCtrl.remove(i);
                      }
          
                  });
          

          【讨论】:

            【解决方案5】:
             this.nav.push(AnotherPage)
             .then(() => {
                const index = this.viewCtrl.index;
                this.nav.remove(index);
             });
            

            【讨论】:

              【解决方案6】:

              您可以通过使用 insertPage 方法修改导航控制器堆栈来做到这一点。

               this.nav.insertPages(0, [{page: your_home_page}, {page: SearchPage}]);
               this.nav.pop();
              

              这里的 0 是指您要插入页面的位置。在上面的代码中,0 将是您的主页,1 将是您的搜索页面。在 DetailPage 中使用此命令导航弹出到 SearchPage。

              更多信息请查看http://ionicframework.com/docs/v2/api/navigation/NavController/

              【讨论】:

              • 谢谢,但是没有效果。它只向后导航一页。 getSearchPage() { this.navCtrl.insertPages(0, [{page: HomePage}, {page: SearchPage}]); this.navCtrl.pop(); }
              • 尝试添加 this.navCtrl.remove(0);在添加其他代码行之前。这有助于在第 0 个位置从堆栈中删除页面
              • 又没有效果了。我很茫然。
              • 在您的问题中您提到了 nav.push。是 nav、Nav 还是 NavController。您是否同时使用 navController 进行推送和弹出?
              • 是的,它是 NavController。我在构造函数public navCtrl: NavController 中定义。我必须在我的帖子中更正上述内容
              【解决方案7】:

              我也遇到了同样的问题并找到了解决方案,它与接受的答案相似,但没有循环和更多细节,所以我希望这有助于某人理解它。

              所以这是流程:

              Home -> navCtrl.push(SearchPage) -> navCtrl.push(ResultPage) -> navCtrl.push(DetailPage)
              

              这里是各自的索引:

              Home: 0
              SearchPage: 1
              ResultPage: 2 
              DetailPage: 3
              

              如果你想从 DetailPage 返回 SearchPage,你基本上是想在 push DetailPage 时从堆栈中删除 ResultPage。

              所以当你推送 DetailPage 时,在 ResultPage 上试试这个代码:

              // First get the index of ResultPage, it should return 2.
              let resultIndex = this.navCtrl.last().index; 
              
              // Push DetailPage.
              this.navCtrl.push(DetailPage).then(() => {
                // Once it's pushed, this block is called and now we can remove the ResultPage from the stack.
                this.navCtrl.remove(resultIndex, 1); 
                // second parameter is optional and defaults to 1, if you want to remove more pages from stack start with the bottom most index you want to remove and pass number of pages you want to remove starting from the given index. 
              
              });
              

              希望这对某个地方的人有所帮助。

              【讨论】:

                【解决方案8】:

                在我的情况下,它就像这样工作

                import { NavController, NavParams } from 'ionic-angular';
                
                export class MyComponent {
                    ...
                    constructor(private navParams:NavParams, private navCtrl:NavController){
                        //If I get the paramater, I know I have to "skip" last view
                        let condition = this.navParams.get("condition");
                        if (condition){
                            this.navCtrl.removeView(this.navCtrl.last());
                        }
                    }
                    ...
                }
                

                因此,无论您是使用软/硬按钮返回还​​是手动 pop(),都会跳过上一个视图。

                【讨论】:

                  【解决方案9】:

                  最好的方法(因为避免页面之间的转换并直接转到倒数第二个页面)是以下解决方案:

                  this.navCtrl.remove(nav.getActive().index - 1, 2)
                  

                  希望对你有用

                  【讨论】:

                    猜你喜欢
                    • 2012-10-12
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2017-01-17
                    • 1970-01-01
                    • 2021-01-06
                    • 2016-10-25
                    • 2016-11-02
                    相关资源
                    最近更新 更多