You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Featrue Request: BuiltIn way to unsubscribe from observables when the component destroyed #20941
[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report
[X ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Component inheritance works pretty well now, so it should be easy to create base class that provides automatic cleanup on destroy: unsubscribing, removing event listeners and disposing other things. Using decorator will lack compile time checks. Base class can also solve other problems: better handling for workarounds with setTimeout, provide state: initialized, destroyed, view initialized, provide support for automation and component-level logging, provide global template variables i.e. expose translation api, console object, enumerations and etc, provide lifecycle callbacks as base class methods without implementing interfaces every time.
@kemsky i don't ask to use decorator. It was just an example of a way that may solve it. I want angular team to add this (base class/decorator/...) to the framework to don't have to redo this each time in each project or depend on a 3rd party library with a decorator approach that (may break) or (is not optimal).
@kemsky Problem with component inheritance is you can only extend a single class, if you want to provide different features they all need to be bundled into it. Not a viable long-term solution (although we're using it to work around the lack of lifecycle observables).
Activity
ericmartinezr commentedon Dec 11, 2017
Is this #10185 what you're asking for?
kemsky commentedon Dec 12, 2017
Component inheritance works pretty well now, so it should be easy to create base class that provides automatic cleanup on destroy: unsubscribing, removing event listeners and disposing other things. Using decorator will lack compile time checks. Base class can also solve other problems: better handling for workarounds with
setTimeout, provide state: initialized, destroyed, view initialized, provide support for automation and component-level logging, provide global template variables i.e. expose translation api, console object, enumerations and etc, provide lifecycle callbacks as base class methods without implementing interfaces every time.nasreddineskandrani commentedon Dec 12, 2017
@kemsky i don't ask to use decorator. It was just an example of a way that may solve it. I want angular team to add this (base class/decorator/...) to the framework to don't have to redo this each time in each project or depend on a 3rd party library with a decorator approach that (may break) or (is not optimal).
@ericmartinezr thanks! i ll close this issue => as duplicate of #10185
note: my coworker found this PR related to the topic:
#20682
insidewhy commentedon Feb 21, 2018
@kemsky Problem with component inheritance is you can only extend a single class, if you want to provide different features they all need to be bundled into it. Not a viable long-term solution (although we're using it to work around the lack of lifecycle observables).
kemsky commentedon Apr 24, 2018
@ohjames, you could try to use TypeScript mixins, but I'm not sure if it works with angular without workarounds.
nasreddineskandrani commentedon Aug 17, 2018
@ohjames since angular team delayed the feature (#20682) for ng7 Ivy, you can use a custom decorator that you create your self https://netbasal.com/automagically-unsubscribe-in-angular-4487e9853a88
personally i am using inheritance for now.
dinohorvat commentedon Mar 27, 2019
Custom decorator won't work well if you build the app with AOT. You still need to declare ngOnDestroy even if its empty.
angular-automatic-lock-bot commentedon Sep 14, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.