angular - 获取 RC0 中的当前页面/ View 名称返回 “t”

标签 angular typescript ionic2 ionic3

我希望处理硬件后退按钮,因为我需要获取当前 View 的名称。

console.log(this.nav.getActive());
 console.log(this.navCtrl.getActive().component.name);

这只是在设备上返回字母 t 但在浏览器中确实有效。

请告诉我如何在设备上获取页面/ View 名称以处理后退按钮

最佳答案

问题是 webpack 在设备上运行应用程序时正在缩小代码(我猜是使用 --prod 标志),这就是为什么在浏览器上工作时名称是正确的,但显示 t 作为在设备上运行应用程序时的名称。 为了避免这种情况,您可以使用 instanceof 运算符来检查页面是否是您要执行某些操作的页面。

这是我在我开发过的一个应用程序中处理后退按钮的方式:

this.platform.registerBackButtonAction(() => {
    this.handleBackButton();
});

然后

private handleBackButton(): void {

    // Prevent to dismiss a modal when another modal is being dismissed
    let ready = true;

    let activePortal = this.ionicApp._loadingPortal.getActive() ||
        this.ionicApp._modalPortal.getActive() ||
        this.ionicApp._toastPortal.getActive() ||
        this.ionicApp._overlayPortal.getActive();

    if (activePortal) {
        ready = false;
        activePortal.dismiss();
        activePortal.onDidDismiss(() => { ready = true; });
        return;
    }

    if (this.menuCtrl.isOpen()) {
        this.menuCtrl.close();
        return;
    }

    let view = this.navCtrl.getActive();
    let page = view ? this.navCtrl.getActive().instance : null;

    if (page && (page instanceof HomePage || page instanceof SignInPage)) {
        this.platform.exitApp();
    }
    else if (this.navCtrl.canGoBack() || view && view.isOverlay) {
        this.navCtrl.pop();
    } else {
        this.accountService.isLoggedIn() ? this.navCtrl.setRoot(HomePage) : this.navCtrl.setRoot(SignInPage);
    }
}

正如您在代码中看到的,我使用 instanceof 运算符检查 View 是否为特定 View

page instanceof HomePage

即使 webpacks 压缩了代码,它也应该可以正常工作。

关于angular - 获取 RC0 中的当前页面/ View 名称返回 “t”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44562225/

相关文章:

.net - 基于角色的导航项

Angular 4 和 Plotly

javascript - 在 Typescript 严格模式函数上访问被调用者?

Angular Firestore 从 Doc 获取单个值

css - ionic 2 : Beta 8 - scrollable content area cutting off button at bottom

angular - 将 ngx-charts 作为单独组件导入 Angular 时出错 : error NG8001: 'ngx-charts-tree-map' is not a known element

html - 在 html 元素上只允许点击事件一次

node.js - 使用 SublimeText2 编译 Typescript

javascript - 十进制一位输入类型只有数字 Ionic 3 最新版本(3.9.2)我正在使用

angular - ionic 2 中的可滚动段(水平)