Skip to content

OnDestroy override issue - Angular 9+ #38043

Closed

Description

@AvshT

🐞 bug report

Affected Package

The issue is caused by package **@angular/core**

Is this a regression?

Yes, the previous version in which this bug was not present was: **Angular 8**

Description

Consider a @component what implements OnDestroy,
on the constructor there is a case I must set the following code:

this.OnDestroy = ()=>{
alert('Invoke');
}

On Angular 8, it use to work, but somehow in Angular 9 (maybe its IVE issue) that not entering to the override func, instead it entering to the initial implemented func.

Can you explain why and if it really a bug?

🌍 Your Environment

Angular Version:




Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.901.6
@angular-devkit/core         9.1.6
@angular-devkit/schematics   9.1.6
@schematics/angular          9.1.6
@schematics/update           0.901.6
rxjs                         6.5.4

Activity

  1. petebacondarwin commented on Jul 14, 2020

    @petebacondarwin
    Contributor

    This is kind of a duplicate of #30497, which is fixed by #35464. But in your case, you appear to be changing the onDestroy() method on the instance, whereas that PR will only allow you to change the method on the prototype.

    I think you could workaround this right now by creating your class like:

    @Component(...)
    export class MyComponent implements NgOnDestroy {
      onDestroy() {
        this._onDestroy();
      }
    
      private _onDestroy() {}
    
      constructor() {
        this._onDestroy = () => {
          alert('Invoke');
        };
      }
    }
  2. added this to the needsTriage milestone on Jul 14, 2020
  3. spock123 commented on Jul 22, 2020

    @spock123

    @petebacondarwin there is no way to use arrow functions in general as lifecycle hooks, is there?

    Is this because of some framework issue (inheritance) or is it a Typescript issue? It's just for aesthetics, doing hooks as regular functions and all other methods as fat arrow functions just looks weird to my ocd brain ;)

  4. petebacondarwin commented on Jul 23, 2020

    @petebacondarwin
    Contributor

    @spock123 - I don't understand the question here. Could you give a concrete example of what is not working for you?

  5. spock123 commented on Jul 23, 2020

    @spock123

    @petebacondarwin I'm sorry I did not intend to hijack this issue but I guess it's somewhat related:

    If I have a component with the following code ngOnInit method:

    ngOnInit = () => console.log('Hello Init');

    • won't be called by the framework.

    However this will be called:

    ngOnInit() { console.log('Hello Init'); }

    I was just wondering if it was somehow possible to use fat arrow functions when creating component lifecycle hooks

  6. petebacondarwin commented on Jul 24, 2020

    @petebacondarwin
    Contributor

    Sorry @spock123 - I am still not sure what the problem is here. The ngOnInit() method must be defined (even after #35464) before the app bootstraps. The following works for me:

    @Component({selector: 'app-root', template: ''})
    export class AppComponent {
      title = 'test-app';
    }
    
    (AppComponent.prototype as any).ngOnInit = () => {
      console.log('init!');
    };
  7. spock123 commented on Jul 24, 2020

    @spock123

    @petebacondarwin thank you for your explanation.

    This syntax is just a bit. .. weird.. having to add it to the prototype is not what I had in mind, I just wanted to use the fat arrow syntax directly in the component when creating component hooks.

    But this is really hijacking this issue, I'm sorry for the derailing. I'll experiment more with how to use fat arrow functions.

    Cheers

  8. petebacondarwin commented on Jul 24, 2020

    @petebacondarwin
    Contributor

    Using it directly in the component is effectively the same as doing it in the constructor. This has two problems:

    1. It only changes the instance of the component and not the prototype, which Ivy will not consider. It only looks at the prototype.
    2. It is too late for Ivy since it reads the prototype of the component at build time and takes a reference to the method.
  9. pkozlowski-opensource commented on Jul 27, 2020

    @pkozlowski-opensource
    Member

    This currently works as designed - I've opened a new feature request with more info: #38241

  10. angular-automatic-lock-bot commented on Aug 27, 2020

    @angular-automatic-lock-bot

    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.

  11. locked and limited conversation to collaborators on Aug 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions