Angular 4组件生命周期 [英] Angular 4 component lifecycle

查看:90
本文介绍了Angular 4组件生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Angular 4公开的组件生命周期挂钩来处理与组件有关的某些信息.
当我尝试搜索时,我能够找到由角度4暴露的生命周期挂钩列表( https ://angular.io/guide/lifecycle-hooks )
我对触发其中一些方法的代码片段更感兴趣. 例如,在Java中,以下代码行将触发"String"类中的构造函数:

I am trying to use the component life cycle hooks that are exposed by Angular 4 to process certain information related to the component.
When I tried to search I was able to find the list of life cycle hooks that are exposed by angular 4 (https://angular.io/guide/lifecycle-hooks)
I am more interested in the code snippet that triggers few of these methods. For example in Java following lines of code would trigger the constructor in 'String' class:

String hotelName = new String("Marriott");

将触发以下生命周期方法的HTML,JavaScript代码是什么

What would be the HTML, JavaScript code that would trigger following life cycle methods:

  • 构造函数
  • ngOnInit
  • ngAfterViewInit
  • ngAfterViewChecked
  • ngOnDestroy

谢谢.

推荐答案

在Angular中,框架将所有生命周期挂钩作为更改检测的一部分来触发.要了解有关该过程的更多信息,请阅读:

In Angular all lifecycle hooks are triggered as part of change detection by the framework. To learn more about the process read:

  • Everything you need to know about change detection in Angular
  • These 5 articles will make you an Angular Change Detection expert

例如,在Java中,以下代码行将触发 "String"类中的构造函数:

For example in Java following lines of code would trigger the constructor in 'String' class:

构造函数不是生命周期挂钩,并且在创建组件时被触发.这与您所展示的机制类似,其中包括使用new调用组件构造函数.

Constructor is not a lifecycle hook and is triggered when a component is created. It's similar mechanics to the one you showed which includes calling a component constructor with new.

要了解更多信息,请阅读:

To learn more read:

这篇关于Angular 4组件生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆