Ionic4中内置的生命周期函数:
ionViewWillEnter —当进入一个页面时触发(如果它从堆栈返回)

ionViewDidEnter —进入后触发

ionViewWillLeave —如果页面将离开触发

ionViewDidLeave — 在页面离开后触发

ionViewWillUnload — 在Angular中没有触发,因为这里你必须使用ngOnDestroy
Ionic4中使用Angular生命周期函数:

1、Ionic4中的生命周期函数ngOnChanges 当被绑定的输入属性的值发生变化时调用(父子组件传值的时候会触发

2、Ionic4中的生命周期函数ngOnInit 请求数据一般放在这个里面 (重要*)

3、Ionic4中的生命周期函数ngDoCheck 检测,并在发生 Angular 无法或不愿意自己检测的变化时作出反应

4、Ionic4中的生命周期函数 ngAfterContentInit 当把内容投影进组件之后调用

5、Ionic4中的生命周期函数 ngAfterContentChecked 每次完成被投影组件内容的变更检测之后调用

6、Ionic4中的生命周期函数 ngAfterViewInit  初始化完组件视图及其子视图之后调用(dom操作放在这个里面) (重要)

7、Ionic4中的生命周期函数 ngAfterViewInit  每次做完组件视图和子视图的变更检测之后调用

8、Ionic4中的生命周期函数  ngOnDestroy  组件销毁后执行 (重要)

  constructor() {       console.log('00构造函数执行了---除了使用简单的值对局部变量进行初始化之外,什么都不应该做')     }

    ngOnChanges() {       console.log('01ngOnChages执行了---当被绑定的输入属性的值发生变化时调用(父子组件传值的时候会触发)');     }

    ngOnInit() {         console.log('02ngOnInit执行了--- 请求数据一般放在这个里面');        }

    ngDoCheck() {         //写一些自定义的操作         console.log('03ngDoCheck执行了---检测,并在发生 Angular 无法或不愿意自己检测的变化时作出反应');         if(this.userinfo!==this.oldUserinfo){             console.log(你从${this.oldUserinfo}改成${this.userinfo});             this.oldUserinfo = this.userinfo;         }else{    console.log("数据没有变化");         }     }

    ngAfterContentInit() {         console.log('04ngAfterContentInit执行了---当把内容投影进组件之后调用');     }

    ngAfterContentChecked() {         console.log('05ngAfterContentChecked执行了---每次完成被投影组件内容的变更检测之后调用');     }

    ngAfterViewInit(): void {            console.log('06 ngAfterViewInit执行了----初始化完组件视图及其子视图之后调用(dom操作放在这个里面)');     }

    ngAfterViewChecked() {      console.log('07ngAfterViewChecked执行了----每次做完组件视图和子视图的变更检测之后调用');     }

    ngOnDestroy() {         console.log('08ngOnDestroy执行了····');     }

Ionic4内置生命周期函数使用demo
import { Component, OnInit } from '@angular/core';

import { StorageService } from '../services/storage.service';

@Component({

  selector: 'app-tab4',

  templateUrl: './tab4.page.html',

  styleUrls: ['./tab4.page.scss'],

})

export class Tab4Page implements OnInit {

  public userinfo:any='';

  constructor(public storage:StorageService) {



  }

  ngOnInit() { }

  ionViewWillEnter(){

    console.log('ionViewWillEnter');

  }

  ionViewDidEnter(){console.log('ionViewDidEnter');  }

}
Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐