I want to execute some code every time the page changes.
I could add add an ngOnDestroy
method to every page. It appears that I could use Ionic 2 page lifecycle hooks (e.g. ionViewDidUnload
) for the exact same effect, but I haven't bothered to test that.I would rather add a single method to the main app class.
I see that you can subscribe to Angular 2 Router events. How do I translate that for use in Ionic 2? I'm getting an error with import { Router } from '@angular/router;
in the first place:
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"]
}
};
It would make more sense to me anyways if there is a way to use the Nav
or NavController
service from ionic-angular. Is there a way to do that?
Ionic2
doesn't use Angular2's Router
. They have their own implementation NavController
.
I see that you can subscribe to Angular 2 Router events. How do I translate that for use in Ionic 2?
You can merge all the NavController Events
and subscribe to it.
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);
});