web前端全系列 教程
1839个小节阅读:2244.2k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
挂载阶段中的生命周期函数在组件的整个生命周期中只执行一次,页面更新时不再执行。
constructor
在组件类被实例化时执行, 可以给它传递参数从而获取组件想要使用的服务。
xxxxxxxxxx
export class AppComponent {
constructor(private te:
ElementRef) {
console.log(te)
}
}
ngOnInit
ngOnInit 在组件首次接收到输入属性值后执行,如果该组件没有绑定输入属性,也会执行。
xxxxxxxxxx
<!-- app.component.html -->
<app-init-test name="张三"></app-init-test>
xxxxxxxxxx
//组件类文件init-test.component.ts
export class InitTestComponent implements OnInit {
@Input() name = ''
constructor() { }
ngOnInit(): void {
console.log('ngOnInit',this.name)
}
}
ngAfterContentInit
当投影内容初始渲染完成后调用。
xxxxxxxxxx
<!-- app.component.html -->
<app-init-test >
<div #box>hello</div>
</app-init-test>
xxxxxxxxxx
//组件类文件init-test.component.ts
export class InitTestComponent implements AfterContentInit {
//使用@ContentChild获取ng-content的内容
@ContentChild('box') box:ElementRef<HTMLDivElement>|undefined
constructor() { }
ngAfterContentInit(): void {
console.log(this.box,'ngAfterContentInit')
//<div #box>hello</div>
}
}
ngAfterViewInit
当组件跟其子组件视图渲染完成后调用。
xxxxxxxxxx
<!-- 组件模板文件 -->
<p #container>
init-test works!
</p>
xxxxxxxxxx
//组件类文件
export class InitTestComponent implements AfterViewInit{
@ViewChild('container') container:ElementRef<HTMLParagraphElement>|undefined
constructor() { }
ngAfterViewInit(): void {
console.log(this.container,'ngAfterViewInit')
}
}
实时效果反馈
1.下列描述错误的是:
A construct
在组件实例化的时候被调用
B ngOnInit
钩子函数中还不可以获取到输入属性
C ngOnInit
在生命周期中只会被调用一次
D ngAfterViewInit
在生命周期中只会被调用一次
答案
1=>B