Giter VIP home page Giter VIP logo

Comments (2)

fasttime avatar fasttime commented on June 18, 2024

In fact, Angular calls ngOnDestroy only once per object, regardless of class hierarchy.
Since your AppComponent extends two classes, both implementing ngOnDestroy, only the first implementation is being called.
This is a known caveat: there is no concise method to change this behavior.
If what you need is actually calling multiple base implementations in turn, I'd suggest writing your own ngOnDestroy.

  ngOnInit(): void {
    // Do something and navigate to other page
    // No need to call ngOnDestroy directly here!
  }
  
  ngOnDestroy(): void {
    super.class(ClassA).ngOnDestroy();
    super.class(ClassB).ngOnDestroy();
  }

You could also iterate over the direct base classes and call ngOnDestroy in a loop.

import { classes, getPrototypeListOf } from 'polytype';

  ...

  ngOnDestroy(): void {
    for (const baseClass of getPrototypeListOf(AppComponent)) {
      super.class(baseClass).ngOnDestroy();
    }
  }

  ...

Finally, you could create a custom type decorator to annotate your class instead of writing a new method.
This is especially useful if you have multiple places in code that rely on the same pattern.

import { OnDestroy } from '@angular/core';
import { getPrototypeListOf } from 'polytype';

export function DestroyBases<T extends { new(...args: any[]): { } }>(derivedClass: T) {
    const prototypeList = getPrototypeListOf(derivedClass.prototype);
    return class extends derivedClass implements OnDestroy {
        ngOnDestroy(): void {
            for (const prototype of prototypeList) {
                if ('ngOnDestroy' in prototype) {
                    prototype.ngOnDestroy.call(this);
                }
            }
        }
    };
}

Then just add the annotation @DestroyBases to AppComponent.

  @Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.sass']
  })
  @DestroyBases
  export class AppComponent extends classes(ClassA, ClassB)  {

Please, let me know if this does not solve your problem or if you need more help.

from polytype.

franciscojoseramosespinar avatar franciscojoseramosespinar commented on June 18, 2024

Decorator is the best idea and works correctly, thank you so much.

from polytype.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.