ionic 2 页面更改事件
ionic 2 page change event
我想在每次页面更改时执行一些代码。
我可以向每个页面添加 ngOnDestroy
方法。看来我可以使用 Ionic 2 页面 lifecycle hooks(例如 ionViewDidUnload
)来获得完全相同的效果,但我没有费心去测试 that.I 宁愿在主页面中添加一个方法应用 class.
我看到你可以订阅 Angular 2 。我如何翻译它以便在 Ionic 2 中使用?我首先遇到 import { Router } from '@angular/router;
错误:
TypeScript error: <path>/node_modules/@angular/router/src/common_router_providers.d.ts(9,55): Error TS2305: Module '"<path>/node_modules/@angular/core/index"' has no exported member 'NgModuleFactoryLoader'.
TypeScript error: <path>/node_modules/@angular/router/src/router.d.ts(14,39): Error TS2305: Module '"<path>/node_modules/@angular/core/index"' has no exported member 'NgModuleFactoryLoader'.
TypeScript error: <path>/node_modules/@angular/router/src/router_module.d.ts(9,10): Error TS2305: Module '"<path>/node_modules/@angular/core/index"' has no exported member 'ModuleWithProviders'.
webpack 配置
var path = require('path');
module.exports = {
entry: [
path.normalize('es6-shim/es6-shim.min'),
'reflect-metadata',
path.normalize('zone.js/dist/zone'),
path.resolve('app/app.ts')
],
output: {
path: path.resolve('www/build/js'),
filename: 'app.bundle.js',
pathinfo: false // show module paths in the bundle, handy for debugging
},
module: {
loaders: [
{
test: /\.ts$/,
loader: 'awesome-typescript',
query: {
doTypeCheck: true,
resolveGlobs: false,
externals: ['typings/browser.d.ts']
},
include: path.resolve('app'),
exclude: /node_modules/
}
],
noParse: [
/es6-shim/,
/reflect-metadata/,
/zone\.js(\/|\)dist(\/|\)zone/
]
},
resolve: {
alias: {
'angular2': path.resolve('node_modules/angular2')
},
extensions: ["", ".js", ".ts"]
}
};
无论如何,如果有一种方法可以使用 ionic-angular 的 Nav
或 NavController
服务,那对我来说会更有意义。有办法吗?
第一件事路由器存在于 @angular/router
模块中。
对于侦听路由更改事件,您可以在路由器 changes
对象上进行订阅。
代码
class MyRouteEventClass {
constructor(private router: Router) {
router.changes.subscribe((val) => {
/* Awesome code here */
}
)
}
}
Ionic2
不使用 Angular2's Router
。他们有自己的实现 NavController
.
I see that you can subscribe to Angular 2 Router events. How do I
translate that for use in Ionic 2?
您可以合并所有NavController Events
并订阅。
allEvents = Observable.merge(
this.navController.viewDidLoad,
this.navController.viewWillEnter,
this.navController.viewDidEnter,
this.navController.viewWillLeave,
this.navController.viewDidLeave,
this.navController.viewWillUnload);
allEvents.subscribe((e) => {
console.log(e);
});
另一种选择是创建一个超级 class,您可以在其中使用 ionViewDidUnload
方法(或任何其他生命周期挂钩),如下所示:
import { Events } from 'ionic-angular';
export class BasePage {
constructor(public eventsCtrl: Events) { }
ionViewDidEnter() {
this.eventsCtrl.publish('page:load');
}
ionViewDidUnload() {
this.eventsCtrl.publish('page:unload');
}
}
然后在每一页中你只需要 extend
BasePage
@Component({
templateUrl: 'build/pages/my-page/my-page.html',
})
export class MyPage extends BasePage {
constructor(private platform: Platform,
private nav: NavController,
private menuCtrl: MenuController,
...,
eventsCtrl: Events) //notice that this one does not have the private/public keyword
{
// Due to an issue in angular, by now you must send the dependency to the super class
// https://github.com/angular/angular/issues/5155
super(eventsCtrl);
//...
}
然后,您可以在主 app.ts
文件中添加类似这样的方法来响应这些事件:
private initializeEventHandlers() {
this.events.subscribe('page:load', () => {
// your code...
});
this.events.subscribe('page:unload', () => {
// your code...
});
}
您可以使用普通的旧 Javascript 放置在 app.component.ts 中。假设你使用 Ionic2-RC0:
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from 'ionic-native';
import { YourPage } from '../pages/yourpage/yourpage';
@Component({
template: `<ion-nav [root]="rootPage"></ion-nav>`
})
export class MyApp {
rootPage = YourPage;
constructor(platform: Platform) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
window.addEventListener('load', () =>{
console.log('page changed');
});
});
}
每次使用 NavController 更改页面时,您都会在控制台中打印出 page changed
。
在 Ionic2+ 中,您可以简单地订阅要执行代码的事件,如下所示:
this.navCtrl.ionViewWillUnload.subscribe(view => {
console.log(view);
});
您可以订阅所有Lifecycle Events
从 Ionic 3.6 开始,您可以使用 App 组件订阅应用程序范围内的页面更改事件,如以下文档所述:https://ionicframework.com/docs/api/components/app/App/
例如,如果您想使用 GA cordova 插件跟踪 Google Analytics 中的每个视图更改,您可以像这样修改 app.component.ts:
constructor(private app: App, private platform: Platform, private ga: GoogleAnalytics, ...) {
this.platform.ready().then(() => {
this.ga.startTrackerWithId('UA-XXX').then(() => {
this.app.viewDidEnter.subscribe((evt) => {
// evt.instance is the Ionic page component
this.ga.trackView(evt.instance.title);
});
}).catch(e => console.log('Doh', e));
}
}
我想在每次页面更改时执行一些代码。
我可以向每个页面添加 ngOnDestroy
方法。看来我可以使用 Ionic 2 页面 lifecycle hooks(例如 ionViewDidUnload
)来获得完全相同的效果,但我没有费心去测试 that.I 宁愿在主页面中添加一个方法应用 class.
我看到你可以订阅 Angular 2 import { Router } from '@angular/router;
错误:
TypeScript error: <path>/node_modules/@angular/router/src/common_router_providers.d.ts(9,55): Error TS2305: Module '"<path>/node_modules/@angular/core/index"' has no exported member 'NgModuleFactoryLoader'.
TypeScript error: <path>/node_modules/@angular/router/src/router.d.ts(14,39): Error TS2305: Module '"<path>/node_modules/@angular/core/index"' has no exported member 'NgModuleFactoryLoader'.
TypeScript error: <path>/node_modules/@angular/router/src/router_module.d.ts(9,10): Error TS2305: Module '"<path>/node_modules/@angular/core/index"' has no exported member 'ModuleWithProviders'.
webpack 配置
var path = require('path');
module.exports = {
entry: [
path.normalize('es6-shim/es6-shim.min'),
'reflect-metadata',
path.normalize('zone.js/dist/zone'),
path.resolve('app/app.ts')
],
output: {
path: path.resolve('www/build/js'),
filename: 'app.bundle.js',
pathinfo: false // show module paths in the bundle, handy for debugging
},
module: {
loaders: [
{
test: /\.ts$/,
loader: 'awesome-typescript',
query: {
doTypeCheck: true,
resolveGlobs: false,
externals: ['typings/browser.d.ts']
},
include: path.resolve('app'),
exclude: /node_modules/
}
],
noParse: [
/es6-shim/,
/reflect-metadata/,
/zone\.js(\/|\)dist(\/|\)zone/
]
},
resolve: {
alias: {
'angular2': path.resolve('node_modules/angular2')
},
extensions: ["", ".js", ".ts"]
}
};
无论如何,如果有一种方法可以使用 ionic-angular 的 Nav
或 NavController
服务,那对我来说会更有意义。有办法吗?
第一件事路由器存在于 @angular/router
模块中。
对于侦听路由更改事件,您可以在路由器 changes
对象上进行订阅。
代码
class MyRouteEventClass {
constructor(private router: Router) {
router.changes.subscribe((val) => {
/* Awesome code here */
}
)
}
}
Ionic2
不使用 Angular2's Router
。他们有自己的实现 NavController
.
I see that you can subscribe to Angular 2 Router events. How do I translate that for use in Ionic 2?
您可以合并所有NavController Events
并订阅。
allEvents = Observable.merge(
this.navController.viewDidLoad,
this.navController.viewWillEnter,
this.navController.viewDidEnter,
this.navController.viewWillLeave,
this.navController.viewDidLeave,
this.navController.viewWillUnload);
allEvents.subscribe((e) => {
console.log(e);
});
另一种选择是创建一个超级 class,您可以在其中使用 ionViewDidUnload
方法(或任何其他生命周期挂钩),如下所示:
import { Events } from 'ionic-angular';
export class BasePage {
constructor(public eventsCtrl: Events) { }
ionViewDidEnter() {
this.eventsCtrl.publish('page:load');
}
ionViewDidUnload() {
this.eventsCtrl.publish('page:unload');
}
}
然后在每一页中你只需要 extend
BasePage
@Component({
templateUrl: 'build/pages/my-page/my-page.html',
})
export class MyPage extends BasePage {
constructor(private platform: Platform,
private nav: NavController,
private menuCtrl: MenuController,
...,
eventsCtrl: Events) //notice that this one does not have the private/public keyword
{
// Due to an issue in angular, by now you must send the dependency to the super class
// https://github.com/angular/angular/issues/5155
super(eventsCtrl);
//...
}
然后,您可以在主 app.ts
文件中添加类似这样的方法来响应这些事件:
private initializeEventHandlers() {
this.events.subscribe('page:load', () => {
// your code...
});
this.events.subscribe('page:unload', () => {
// your code...
});
}
您可以使用普通的旧 Javascript 放置在 app.component.ts 中。假设你使用 Ionic2-RC0:
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from 'ionic-native';
import { YourPage } from '../pages/yourpage/yourpage';
@Component({
template: `<ion-nav [root]="rootPage"></ion-nav>`
})
export class MyApp {
rootPage = YourPage;
constructor(platform: Platform) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
window.addEventListener('load', () =>{
console.log('page changed');
});
});
}
每次使用 NavController 更改页面时,您都会在控制台中打印出 page changed
。
在 Ionic2+ 中,您可以简单地订阅要执行代码的事件,如下所示:
this.navCtrl.ionViewWillUnload.subscribe(view => {
console.log(view);
});
您可以订阅所有Lifecycle Events
从 Ionic 3.6 开始,您可以使用 App 组件订阅应用程序范围内的页面更改事件,如以下文档所述:https://ionicframework.com/docs/api/components/app/App/
例如,如果您想使用 GA cordova 插件跟踪 Google Analytics 中的每个视图更改,您可以像这样修改 app.component.ts:
constructor(private app: App, private platform: Platform, private ga: GoogleAnalytics, ...) {
this.platform.ready().then(() => {
this.ga.startTrackerWithId('UA-XXX').then(() => {
this.app.viewDidEnter.subscribe((evt) => {
// evt.instance is the Ionic page component
this.ga.trackView(evt.instance.title);
});
}).catch(e => console.log('Doh', e));
}
}