简体   繁体   English

Ionic 2 - 如何处理backButton

[英]Ionic 2 - how to handle backButton

I want to add a native backButton event handler in Ionic2. 我想在Ionic2中添加一个原生的backButton事件处理程序。 Problem is that Ionic2 documentation says to use registerBackButtonAction method of Platform instance, but there's no such method. 问题是Ionic2文档说使用Platform实例的registerBackButtonAction方法,但是没有这样的方法。

Instead of registerBackButtonAction , there is backButton property which is EventEmitter type (but it's not working, or I do not know how to subscribe). 而不是registerBackButtonAction ,有backButton属性,它是EventEmitter类型(但它不起作用,或者我不知道如何订阅)。

Any ideas? 有任何想法吗? Marcin 马尔钦

You can handle with angular2 routerOnActivate.. location .back() 你可以使用angular2 routerOnActivate .. location .back()来处理

     <button danger (click)="navigateBack()">Danger</button>

Your ts file contains 你的ts文件包含

    // Import    
    import {ComponentInstruction} from '@angular/router-deprecated';

    // Class
    export class ViewImagePge {
    public prev: ComponentInstruction;
    } 

    public routerOnActivate(next: ComponentInstruction, prev: ComponentInstruction): void {
      this.prev = prevInstruction;
    }

    // navigateBack() Function

    public navigateBack(): void {
    if(this.prev && this.prev.routeName === 'Foo') {
          this.location.back();
        } else {

          // can keep default path

          this.router.navigate(['/Foo']);
        }
    }

i was dealing with hardware backbuttons few days and now, after asing mike (from ionic) i know whats the problem. 几天后我正在处理硬件背板,现在,在离开迈克(来自离子)之后,我知道问题是什么。

The problem is that they dont have priorities hierachy. 问题是他们没有优先级层次。 By default there is only one action with default priority value (0) and that action handle everything. 默认情况下,只有一个操作具有默认优先级值(0),该操作处理所有操作。

So if you want to add custom actions you need to add the default code action in your action if not you will be overriding and will not close sidemenu for example. 因此,如果您想添加自定义操作,则需要在操作中添加默认代码操作,否则您将覆盖并且不会关闭sidemenu。

I have a demo on github with a backbutton service and a sidemenu with two pages. 我在github上有一个带有后退按钮服务的演示和一个有两页的侧面菜单。 In page 1 i have a double click to exit app and on page 2 the default, on both pages i handle the sidemenu to close instead of show the toast. 在第1页中,我双击退出应用程序,在第2页上默认,在两个页面上我处理侧面菜单关闭而不是显示吐司。

For the moment not handle modals but i want to add it asap. 暂时不处理模态,但我想尽快添加它。

hope this can help you. 希望这可以帮到你。

You can use below code. 您可以使用以下代码。 It works on back button pressed of mobile device 它适用于移动设备按下的后退按钮

platform.registerBackButtonAction(function () {

    if(menu.isOpen())
              {
                menu.close();
              }else
              {
                nav.pop();  
              }          
    }, 100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM