Skip to content

Lab 7: Lifecycle Hooks

  • Move code from the constructor to ngOnInit
  • In album.component.ts, add a console.log for the album variable.
  • Notice that album shows undefined even though there is an album in the input.

Step 2: Move the Code from the Constructor to ngOnInit

Section titled “Step 2: Move the Code from the Constructor to ngOnInit”
  • Move the constructor content into ngOnInit and remove the constructor.
  • Do the same for the album-list.component.ts and album-of-the-day.component.ts components.
  • Verify that everything works correctly.
  • You can see now that your IDE is showing you some errors:
  • Here is an example with IntelliJ IDEA: Component tree
  • We need to tell the IDE that the albums variable will be initialized in the ngOnInit method.
  • To do so, we can use the ! operator to tell the IDE that the variable will be initialized later.
albumOfTheDay!: Album