'Angular ERROR TypeError: Cannot read properties of undefined (reading 'onDestroy')

I have problem. Angular is sometimes showing error ERROR TypeError: Cannot read properties of undefined (reading 'onDestroy')

I am getting this error when I am running ng serve, but if I use angular universal, then page is sometimes loading properly and sometimes it is showing same error. I found out, that this error started when I started making modularization of my app.

Angular version 13.3



Solution 1:[1]

I had the same issue. Try to remove the .angular directory! worked for me.

Solution 2:[2]

This happened when I was trying to use components in an ngSwitchCase that rendered ng-templates with components in them.

The components that did not implement ngOnDestroy would give me this error.

short answer, try adding ngOndestroy to components that might be rendering on the fly?

Solution 3:[3]

I found out solution for this hard and not typical bug. It is happening if you don't finish your modularization, but part of code you are using as modules and part as component imports.

Components which aren't modularized and used in app.component.html different than

<router-outlet></router-outlet>

are causing this error.

for my example it was

<app-navbar></app-navbar>

What is funny? If you have this error, and you add to navbar f.e. <div></div> and then remove it app is starting to work normally.

What is the solution? You have to modularize all components used in app.component.html properly.

Solution 4:[4]

I had the same problem. in my case because I defined my model without any default values and bind it to HTML like this myModel.value. so because the myModel.value is not value, I got that error. I replaced it with myModel?.value and the error fixed

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Faisal Alashir
Solution 2 Jason
Solution 3 J4mJeMax
Solution 4 M.R.Sadeghi