angualr4生命周期钩子
发布时间:2020-12-17 08:55:43 所属栏目:安全 来源:网络整理
导读:理解 Angular提供了生命周期钩子,把这些关键生命时刻暴露出来,赋予我们在它们发生时采取行动的能力。可以将钩子函数理解为在合适的时候做合适的事情。 钩子函数 ng4主要提供了8个钩子函数: ngOnchanges @input属性(输入属性)发生变化时,会调用。非此属性
组件模板 <div class="panel-body"> <input type="text" [(ngModel)]="name"> {{name}} <son [name]="name"></son> </div> 组件 @Component({ selector: 'father',templateUrl: './father.component.html',styleUrls: ['./father.component.scss'] }) export class FatherComponent implements OnInit { public name:string; constructor() { } ngOnInit() { console.log("父组件ngOninit"); } ngOnchanges(){ console.log("父组件ngonchanges"); } ngDoCheck (){ console.log("父组件ngDocheck") } ngAfterContentInit(){ console.log("父组件ngAfterContentInit") } ngAfterContentChecked(){ console.log("父组件ngAfterContentChecked") } ngAfterViewInit(){ console.log("父组件ngAfterViewInit") } ngAfterViewChecked(){ console.log("父组件ngAfterViewChecked") } } 子组件 @Component({ selector: 'son',templateUrl: './son.component.html',styleUrls: ['./son.component.scss'] }) export class SonComponent implements OnInit { @Input() name:string; constructor() { } ngOnInit() { console.log("子组件ngOninit"); } ngOnChanges (){ console.log("子组件ngonchanges"); } ngDoCheck (){ console.log("子组件ngDocheck") } ngAfterContentInit(){ console.log("子组件ngAfterContentInit") } ngAfterContentChecked(){ console.log("子组件ngAfterContentChecked") } ngAfterViewInit(){ console.log("子组件ngAfterViewInit") } ngAfterViewChecked(){ console.log("子组件ngAfterViewChecked") } } 看打印结果: 当在父组件的input中输入内容时,会打印如下结果: 看到有人说只有当使用内容投影时才会调用ngAfterConentChecked,当上面的里面的代码很显然是没用ng-content的,不知道该怎么解释这个ngAfterConentChecked。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容