angualr4基础之组件生命周期
来源:互联网 发布:层次方框图软件 编辑:程序博客网 时间:2024/06/05 22:42
组件生命周期
//这个顺序是按照执行的先后排列的constructor:构造器函数,一般用于注入服务ngOnChanges:检测到输入数据变化,首次触发发生在ngOnInit前。注意对象的属性发生变化时监听不到ngOnInit:组件初始化,通常会设置一些初始值ngDoCheck:手动触发更新检查 ngAfterContentInit:内容初始化到组件之后ngAfterContentChecked:内容变更检测之后ngAfterViewInit:视图 初始化之后ngAfterViewChecked:视图发生变化检测之后,这个可以用来保证用户视图的及时更新ngOnDestroy:组件注销时的清理工作,通常用于移除事件监听,退订可观察对象等
- ngOnChanges
- 在父组件初始化或修改子组件的输入参数时调用。
<!--父组件-->import { Component } from '@angular/core';@Component({...})export class AppComponent { greeting:string = "Hello";//当这个对象变更时,会调用子组件的ngonchange钩子 user:{name:string} = {name: "Tom"}; //当name变更时,不会调用子组件的ngonchange钩子,因为他变更的是name属性而不是user对象。 constructor(){}}<!--html--><div class="parent"> <h2>我是父组件</h2> <div> 问候语:<input type="text" [(ngModel)]="greeting">//双向绑定 </div> <div> 姓名:<input type="text" [(ngModel)]="user.name">//双向绑定 </div> <app-child [greeting]="greeting" [user]="user"></app-child>//将值传入子组件,当值改变时会调用子组件的ngonchange钩子</div>
- 变更检测 ngDoCheck (依赖于zone.js)
- Default策略
- OnPush策略
- 所有的事件都会触发变更检测机制,不分原生还是自己写的
- 所有带有check字样的钩子都会这样,只要有一点变动就会调用。除非这个组件被销毁。(在两个input框之间切换都回调用)
//跟上边的代码一样,但是当name改变时,也会调用ngDoCheck方法钩子。import {Component, OnInit, Input, OnChanges, SimpleChanges, DoCheck} from '@angular/core';@Component({ selector: 'app-child', templateUrl: './child.component.html', styleUrls: ['./child.component.css']})export class ChildComponent implements OnInit, OnChanges, DoCheck { @Input() greeting:string; @Input() user:{name:string}; message:string = "初始化消息"; oldUsername:string; changeDetected:boolean = false; noChangeCount:number = 0; constructor() { } ngOnInit() { } ngOnChanges(changes: SimpleChanges): void { console.log(JSON.stringify(changes, null, 2)); } ngDoCheck(): void {//子组件里的变更检测, if(this.user.name !== this.oldUsername) { this.changeDetected = true; console.log("DoCheck:user.name从"+this.oldUsername+"变为"+this.user.name); this.oldUsername = this.user.name; } if(this.changeDetected) { this.noChangeCount = 0; }else{ this.noChangeCount = this.noChangeCount + 1; console.log("DoCheck:user.name没变化时ngDoCheck方法已经被调用"+this.noChangeCount+"次") } this.changeDetected = false; }}
- View钩子
- 在父组件里可以通过@ViewChild获得子组件的引用。调用子组件方法
<!--父组件-->import {Component, OnInit, ViewChild, AfterViewInit, AfterViewChecked} from "@angular/core";import {ChildComponent} from "./child/child.component";@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css']})export class AppComponent implements OnInit, AfterViewInit, @ViewChild("child1")//引入子组件 child1:ChildComponent;//定义子组件对象 message:string; constructor(){ } ngAfterViewInit(): void {//要想执行这个,必须等子组件全部加载完毕。 console.log("父组件的视图初始化完毕"); //在ngAfterViewInit里边不能写赋值等操作,要想进行这些操作必须写在settimeout里边 setTimeout(() => { this.message = "Hello"; },0); } ngAfterViewChecked(): void { console.log("父组件的视图变更检测完毕"); } ngOnInit(): void { setInterval(() => { this.child1.greeting("Tom");//调用子组件里的方法 }, 5000); }<!--子组件-->import {Component, OnInit, AfterViewChecked, AfterViewInit} from '@angular/core';@Component({ selector: 'app-child', templateUrl: './child.component.html', styleUrls: ['./child.component.css']})export class ChildComponent implements OnInit, AfterViewInit, AfterViewChecked { ngAfterViewInit(): void { console.log("子组件的视图初始化完毕"); } ngAfterViewChecked(): void { console.log("子组件的视图变更检测完毕"); } constructor() { } ngOnInit() { } greeting(name:string) { console.log("hello "+name); }}
- ngConent指令
- ngConent指令用于子组件,可以将父组件的内容投影到子组件
<!--父组件--><app-child><div class="header">这是头部,这个div是父组件投影到子组件的</div<div class="footer">这是底部,这个div是父组件投影到子组件的</div></app-child><!--子组件--><ng-content select=".header"></ng-content><ng-content select=".footer"></ng-content>
- AfterContentInit,
AfterContentChecked, AfterViewInit
父组件//在这里边可以对属性内容进行变更ngAfterContentInit(): void { console.log("父组件投影内容初始化完毕");//1 this.message = "hello world"; } ngAfterContentChecked(): void { console.log("父组件投影内容变更检测完毕");//2 } ngAfterViewInit(): void { console.log("父组件视图内容初始化完毕");//5 } 子组件 ngAfterContentInit(): void { console.log("子组件投影内容初始化完毕");//3 this.message = "hello world"; } ngAfterContentChecked(): void { console.log("子组件投影内容变更检测完毕");//4 }
- 销毁ngOnDestroy
- 在路由跳转时会执行的钩子。销毁之前的组件。
阅读全文
0 0
- angualr4基础之组件生命周期
- Android基础之组件生命周期
- 组件间通信 angualr4中间人模式
- Android基础之四大基本组件介绍与生命周期
- Android基础之四大基本组件介绍与生命周期
- Android基础之四大基本组件介绍与生命周期
- Android基础之四大根本组件介绍与生命周期
- ReactJs之组件生命周期
- android组件之Activity生命周期
- Android组件之Service生命周期
- 四大组件之activity(生命周期)
- 10、react之 组件生命周期
- vue之组件的生命周期
- 野人学Android基础篇之四大组件第一课--Activity的生命周期
- Android 四大组件基础 Activity 生命周期
- Vue基础之组件
- Android基础之Activity生命周期
- ASP.NET组件设计之生命周期详解
- 生产者与消费者问题
- 后台处理请求时间过长导致页面超时
- Python 列表生成式
- cmder使用配置记录
- Vue 中阻止冒泡和默认事件
- angualr4基础之组件生命周期
- 分类器评价指标——ROC曲线和AUC值
- 《PHP MySQL和WEB开发》笔记第十一章
- JAVA开发中字节转换问题(byte数组byte数组、合并、转换16进制)
- Eclipse:An internal error occurred during: "Build Project". GC overhead limit exceeded
- Couldn't reserve space for cygwin's heap, Win32 error 0
- 字符串的全排列问题的解决方法
- Ubuntu16.04环境下安装CUDA、cudnn、Caffe、Tensorflow、pytorch
- socket、tcp、udp、http 的认识及区别