我想在每次页面更改时执行一些代码。
我可以为每个页面添加一个ngOnDestroy
方法。似乎我可以使用Ionic 2页面lifecycle hooks(例如ionViewDidUnload
)获得完全相同的效果,但我还没有打算去测试它。我宁愿在主页上添加一个方法app class。
我看到您可以订阅Angular 2 Router events。如何翻译用于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 config
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"]
}
};
如果有办法使用离子角度的Nav
或NavController
服务,那对我来说会更有意义。有没有办法做到这一点?
答案 0 :(得分:11)
Ionic2
不使用Angular2's Router
。他们有自己的实现NavController
。
我看到您可以订阅Angular 2路由器事件。我如何能 翻译用于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);
});
答案 1 :(得分:11)
另一个选择是创建一个超类,你可以使用这样的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...
});
}
答案 2 :(得分:9)
从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));
}
}
答案 3 :(得分:5)
路由器存在于@angular/router
模块中的第一件事。
对于侦听路由更改事件,您可以在路由器changes
对象上进行订阅。
<强>代码强>
class MyRouteEventClass {
constructor(private router: Router) {
router.changes.subscribe((val) => {
/* Awesome code here */
}
)
}
}
答案 4 :(得分:5)
您可以使用普通的旧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
。
答案 5 :(得分:2)
在Ionic2 +中,您可以按如下方式订阅要执行代码的事件:
this.navCtrl.ionViewWillUnload.subscribe(view => {
console.log(view);
});
您可以订阅所有Lifecycle Events