我有一个用例,需要检测向用户显示的当前 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/