问题
In angular application, every time I route to a new page, my earlier page's component is staying in memory. It's not getting destroyed and when I route back to that page it creates a new instance.
Application is not created through angular CLI. It's a custom build application using the webpack configuration provided on angular website. Actually, I am doing performance improvement of the existing angular application and this demo app I created for isolation of issue.
Github https://github.com/bytesofdhiren/angular-memory-issue
Basically, every time I route to home or register page, the application creates a new instance of the register component and home component. It's not destroying the earlier created component.
回答1:
Every component has a lifecycle you need to use:
OnDestroy();
and you need to implements the method ngOnDestroy()
.
Follow this
回答2:
Actually, I tried running the above Github application in production mode and the issue got resolved. But the actual issue in my production application still exists. But that is related to something else. I will post a new question for that.
来源:https://stackoverflow.com/questions/56340306/angular-component-are-not-getting-destroyed