简体   繁体   English

返回时没有调用ngOnInit

[英]ngOnInit not called when coming back

I noticed that the ngOnInit() method gets not called when I come back to the page which is already instanced. 我注意到当我回到已经实例化的页面时,没有调用ngOnInit()方法。 I have to use any other method for this? 我必须使用任何其他方法吗? I need a method which is called everytime when he is visiting the specific page. 我需要一个方法,每次访问特定页面时都会调用它。

EDIT Tested already onPageWillEnter() but it gets not fired in Ionic 2 编辑已经在onPageWillEnter()测试onPageWillEnter()但在Ionic 2中没有被触发

Check Lifecycle Events section in the link. 检查链接中的“ 生命周期事件”部分。

You can use ionic 2 lifecycle hook 您可以使用ionic 2生命周期钩子

ionViewWillEnter(){
 //your methods
}

To recall ngOnInit everytime when page is visiting, you should use ngOnDestroy. 要在每次访问页面时调用ngOnInit,您应该使用ngOnDestroy。 For example, if your component content depends on code in url, you should use OnDestroy, in this way: 例如,如果组件内容依赖于url中的代码,则应以这种方式使用OnDestroy:

export class GRMTasksComponent implements OnInit, OnDestroy {

   subParams: Subscription;

   ngOnInit() {
        this.subParams = this._route.params.subscribe(params => {
           //some code...
      });
   }

   ngOnDestroy() {
        this.subParams.unsubscribe();
   }
}

If you change a route so that only a parameter value changed, then the component is reused. 如果更改路径以便仅更改参数值,则会重用该组件。

You can use 您可以使用

constructor(router:Router) {
  router.params.subscribe(val => myInit());
}

to call your initialization code instead of using ngOnInit() in such cases. 在这种情况下调用初始化代码而不是使用ngOnInit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM