角离子2页变化事件

前端之家收集整理的这篇文章主要介绍了角离子2页变化事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
每次页面改变时,我都要执行一些代码.

我可以添加一个ngOnDestroy方法到每个页面.看来我可以使用Ionic 2第lifecycle hooks页(例如ionViewDidUnload)获得完全相同的效果,但是我没有打扰测试,我宁愿在主应用程序类中添加一个方法.

我看到你可以订阅Angular 2 Router events.如何翻译用于Ionic 2?我从“@ angular / router”导入{Router}时遇到错误首先:

  1. 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'.
  2. 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'.
  3. 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配置

  1. var path = require('path');
  2.  
  3.  
  4. module.exports = {
  5. entry: [
  6. path.normalize('es6-shim/es6-shim.min'),'reflect-Metadata',path.normalize('zone.js/dist/zone'),path.resolve('app/app.ts')
  7. ],output: {
  8. path: path.resolve('www/build/js'),filename: 'app.bundle.js',pathinfo: false // show module paths in the bundle,handy for debugging
  9. },module: {
  10. loaders: [
  11. {
  12. test: /\.ts$/,loader: 'awesome-typescript',query: {
  13. doTypeCheck: true,resolveGlobs: false,externals: ['typings/browser.d.ts']
  14. },include: path.resolve('app'),exclude: /node_modules/
  15. }
  16. ],noParse: [
  17. /es6-shim/,/reflect-Metadata/,/zone\.js(\/|\\)dist(\/|\\)zone/
  18. ]
  19. },resolve: {
  20. alias: {
  21. 'angular2': path.resolve('node_modules/angular2')
  22. },extensions: ["",".js",".ts"]
  23. }
  24. };

如果有一种方法可以使用离子角度的Nav或NavController服务,这将更有意义.有办法吗?

另一个选择是创建一个超类,你可以使用这样的ionViewDidUnload方法(或任何其他生命周期钩子):
  1. import { Events } from 'ionic-angular';
  2.  
  3. export class BasePage {
  4.  
  5. constructor(public eventsCtrl: Events) { }
  6.  
  7. ionViewDidEnter() {
  8. this.eventsCtrl.publish('page:load');
  9. }
  10.  
  11. ionViewDidUnload() {
  12. this.eventsCtrl.publish('page:unload');
  13. }
  14. }

然后在每一页你只需要扩展该BasePage

  1. @Component({
  2. templateUrl: 'build/pages/my-page/my-page.html',})
  3. export class MyPage extends BasePage {
  4.  
  5. constructor(private platform: Platform,private nav: NavController,private menuCtrl: MenuController,...,eventsCtrl: Events) //notice that this one does not have the private/public keyword
  6. {
  7.  
  8. // Due to an issue in angular,by now you must send the dependency to the super class
  9. // https://github.com/angular/angular/issues/5155
  10. super(eventsCtrl);
  11.  
  12. //...
  13. }

然后,您可以在主应用程序文件添加像这样的方法来响应这些事件:

  1. private initializeEventHandlers() {
  2.  
  3. this.events.subscribe('page:load',() => {
  4. // your code...
  5. });
  6.  
  7. this.events.subscribe('page:unload',() => {
  8. // your code...
  9. });
  10.  
  11. }

猜你在找的Angularjs相关文章