ionic-framework - 如何在 Ionic 2 中自动检测 View 变化

标签 ionic-framework ionic2

我有一个用例,需要检测向用户显示的当前 View 以向 Google Analytics(分析)报告。我知道Ionic 2's View Lifecycle hooks但这不是我所追求的。

想知道是否有一种方法可以“ Hook ”到 ionic 的导航系统中,以订阅导航事件并在用户每次导航应用程序时获取路线或 View 对象。由于 Ionic 2 没有使用 Angular2 的 Router 我无法使用以下内容:

export class AppComponent {
    private currentRoute:string;
    constructor(_router:Router,
                _location:Location) {
        _router.events.subscribe((event:Event) => {
            if (event instanceof NavigationEnd) {
                // When the route is '/', location.path actually returns ''.
                let newRoute = this._location.path() || '/';
                // If the route has changed, send the new route to analytics.
            if (this.currentRoute != newRoute) {
                ga('send', 'pageview', newRoute);
                this.currentRoute = newRoute;
            }
        }
        });
    }
}

有人知道如何订阅 Ionic 2 应用程序中的导航更改吗?

截至撰写本文时我当前的设置是:

Ionic Framework Version: 2.0.0-rc.4
Ionic CLI Version: 2.1.13
Ionic App Lib Version: 2.1.7
Ionic App Scripts Version: 0.0.48

最佳答案

您可以订阅docs中描述的App生命周期事件。

更具体地说,您需要从 ionic-angular 注入(inject) App,然后在需要的地方添加如下代码:

this.app.viewWillEnter.subscribe(viewCtrl => {
   console.log('Entering new view')
   console.log(viewCtrl)
})

关于ionic-framework - 如何在 Ionic 2 中自动检测 View 变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41468938/

相关文章:

javascript - ionic android 图标不相同的原始大小/比例

android - Ionic 5 Android 应用程序因使用 Paypal 而不是应用内计费而被拒绝

javascript - 访问父 View 属性的最佳方式(Ionic 2、Angular 2)

ionic-framework - IONIC 2 ionic 选择更改时隐藏和显示文本框

javascript - ng-click 功能仅在第一次有效

javascript - 以 Angular 访问工厂回调函数内部的变量

javascript - ionic : How is it possible to change $ionicPopup subTitle's text programmatically?

javascript - Ionic2 + Angular2 - 试图分配给只读属性

angular - 数组未在 DOM 中拼接,但在控制台中是 - Ionic 2+/Angular2+

cordova - 构建失败的ionic 2