美文网首页
Angular2生命周期

Angular2生命周期

作者: Longwide123 | 来源:发表于2017-04-17 13:47 被阅读327次

测试代码:

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方法

相关文章

  • 浅谈Hooks&&生命周期(2019-03-12

    生命周期 现在流行的前端框架,无论是angular还是React,又或是Angular2以及以上,都由框架自身提供...

  • Angular2 生命周期

    组件生命周期钩子 指令和组件的实例有一个生命周期:新建、更新和销毁。 每个接口都有唯一的一个钩子方法,它们的名字是...

  • 3.5 使用 Angular2 框架

    3.5 使用 Angular2 框架 问题一:如何接入Angular2 框架? 由于 Angular2 项目中采用...

  • Angular2 的生命周期

    组件的生命周期 指令和组件的实例有一个生命周期分别是:新建、更新和销毁。每个接口都有唯一的一个钩子方法,它们的名字...

  • Angular2 -- 生命周期钩子

    组件生命周期钩子 指令和组件的实例有一个生命周期:新建、更新和销毁。每个接口都有唯一的一个钩子方法,它们的名字是由...

  • Angular2 的 View Encapsulation(样式

    Angular2 的 View Encapsulation(样式封装) angular2 版本:2.4.8, 测试...

  • Angular2生命周期

    测试代码: 初始加载的结果: 输入框改变或焦点改变时生命周期 当两个component使用同一个outlet时,从...

  • Angular2语法

    Angular2语法

  • Angular2中如何使用jquery

    Angular2中如何使用jquery Angular2是以TypeScript语言作为默认编码语言,所以你看到的...

  • Angular2编译部署

    Angular2编译部署

网友评论

      本文标题:Angular2生命周期

      本文链接:https://www.haomeiwen.com/subject/kfcbattx.html