Angular2生命周期

测试代码:

import { Component,OnInit,OnDestroy,OnChanges,DoCheck,AfterViewChecked
,AfterViewInit,AfterContentInit,AfterContentChecked } from '@angular/core';
@Component({
  selector: 'my-app',
  template: '<h1>我的第一个 Angular 应用</h1>'
})
export class AppComponent implements OnInit,OnDestroy,OnChanges,DoCheck,AfterViewChecked
,AfterViewInit,AfterContentInit,AfterContentChecked
{
    
    ngOnInit(){
        console.log("ngOnInit");
    }
    ngOnDestroy(){
        console.log("ngOnDestroy");
    }
    ngOnChanges(){
        console.log("ngOnChanges");
    }
    ngDoCheck(){
        console.log("ngDoCheck");
    }
    ngAfterViewChecked(){
        console.log("ngAfterViewChecked");
    }
    ngAfterViewInit(){
        console.log("ngAfterViewInit");
    }
    ngAfterContentInit(){
        console.log("ngAfterContentInit");
    }
    ngAfterContentChecked(){
        console.log("ngAfterContentChecked");
    }
}

初始加载的结果:

app.component.ts:12 ngOnInit
app.component.ts:21 ngDoCheck
app.component.ts:30 ngAfterContentInit
app.component.ts:33 ngAfterContentChecked
app.component.ts:27 ngAfterViewInit
app.component.ts:24 ngAfterViewChecked
:3000/node_modules/@angular/core/bundles/core.umd.js:210 Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
:3000/app/app.component.js:23 ngDoCheck
:3000/app/app.component.js:35 ngAfterContentChecked
:3000/app/app.component.js:26 ngAfterViewChecked
:3000/node_modules/@angular/core/bundles/core.umd.js:210 Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
:3000/app/app.component.js:23 ngDoCheck
:3000/app/app.component.js:35 ngAfterContentChecked
:3000/app/app.component.js:26 ngAfterViewChecked
:3000/node_modules/@angular/core/bundles/core.umd.js:210 Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
:3000/app/app.component.js:23 ngDoCheck
:3000/app/app.component.js:35 ngAfterContentChecked
:3000/app/app.component.js:26 ngAfterViewChecked
:3000/app/app.component.js:23 ngDoCheck
:3000/app/app.component.js:35 ngAfterContentChecked
:3000/app/app.component.js:26 ngAfterViewChecked
:3000/app/app.component.js:23 ngDoCheck
:3000/app/app.component.js:35 ngAfterContentChecked
:3000/app/app.component.js:26 ngAfterViewChecked

输入框改变或焦点改变时生命周期

image.png
ngDoCheck
app.component.ts:36 ngAfterContentChecked
app.component.ts:27 ngAfterViewChecked

当两个component使用同一个outlet时,从一个组件跳到另一个组件时会调用ngOnDestroy方法

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Angular每个组件都存在一个生命周期,从创建,变更到销毁。Angular提供组件生命周期钩子,把这些关键时刻暴...
    4ea0af17fd67阅读 1,745评论 0 3
  • 组件生命周期钩子 指令和组件的实例有一个生命周期:新建、更新和销毁。每个接口都有唯一的一个钩子方法,它们的名字是由...
    Yeaseon阅读 18,140评论 0 13
  • 简述 最近在项目中碰到一个这样的问题,父组件从后台接口获取数据,子组件@Input变量是父组件从后台获取的数据中的...
    w_tiger阅读 5,170评论 0 0
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,812评论 25 709
  • 妹妹前两天从老家带来一点野干菜,是斜蒿和五叶。我迫不及待的用开水淖过之后,拌上调料,开吃,美味,可口,还是记忆中的...
    芣苡zx阅读 1,477评论 0 1