官术网_书友最值得收藏!

The component life cycle

In the life cycle of an Angular component, after instantiating, the component runs a definite path of execution from its beginning until its end. The most basic way to understand this is through observing the following code:

export class HelloComponent implements OnInit, OnDestroy {
constructor() { }

ngOnInit() {
... Some code goes here
}
ngOnDestroy() {
... Some code goes here
}
}

In the preceding example, you can see methods called ngOnInit() and ngOnDestroy; the names are very intuitive, and show us that we have a beginning and an end. The ngOnInit() method is implemented through its OnInit interface, and the same goes for the ngOnDestroy() method. As you saw in the previous chapter, the interfaces in TypeScript are very useful – it's not any different here.

In the following diagram, we will look at the main interfaces that we can implement on a component. In the diagram, after the Constructor() method, there are eight interfaces (also known as hooks); each one is responsible for one thing, at a specific moment:

Angular component life cycle
You can learn more about each interface in the official Angular documentation at  https://angular.io/guide/lifecycle-hooks.

We will not describe the interfaces one by one in this chapter, so as not to overload you, but,  throughout the course of the book, we will use them in the application that we will build. Also, the preceding link includes detailed information about each interface and hook.

主站蜘蛛池模板: 遂宁市| 甘谷县| 双江| 新绛县| 剑河县| 舟山市| 麻江县| 西乌珠穆沁旗| 蛟河市| 全南县| 西畴县| 辽源市| 苏尼特左旗| 射洪县| 个旧市| 崇信县| 康乐县| 门头沟区| 库伦旗| 凤翔县| 内江市| 明光市| 托克逊县| 天峻县| 夏津县| 塘沽区| 江川县| 尤溪县| 景泰县| 全州县| 休宁县| 宜兰县| 贵南县| 自治县| 舞钢市| 沙田区| 安多县| 衡山县| 额尔古纳市| 右玉县| 江陵县|