Ionic 2/Angular 2组件生命周期钩子

Jam*_*ker 3 events hook lifecycle ionic2 angular

我有一个页面中的组件,我需要在每次导航到页面时挂钩.它包含一个mapbox地图,我发现它没有正确调整大小如果用户注册使用注册页面并拉起键盘然后返回到地图页面地图div较小,因为用户拉开了他们的键盘电话.我希望这是有道理的?

我已经尝试了这里列出的所有钩子https://angular.io/guide/lifecycle-hooks,但每次导航到页面时都不会触发.

任何帮助都会很棒.

rob*_*nnn 6

试试:和ionViewDidEnter(),一起@ViewChild.随着@ViewChild您可以访问该组件.

来自Ionic docs:
" ionViewDidEnter()当页面完全进入并且现在是活动页面时将触发.此事件将触发,无论是第一次加载还是缓存页面."

这将使您可以在每次导航页面时调用组件上的函数.

父页TS:

@Component({
    selector: 'parent-page',
    templateURL: 'parent-page.html'
})
export class ParentPage{
    @ViewChild('myComponent') myComponent;

    constructor(){}

    ionViewDidEnter(){
        this.myComponent.someFunc();
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

父页面HTML:

<ion-content>
    ...
    <child-component #myComponent></child-component>
    ...
</ion-content>
Run Code Online (Sandbox Code Playgroud)

  • 页面是一个组成部分 (2认同)