Comments (2)
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.
Decorator is the best idea and works correctly, thank you so much.
from polytype.
Related Issues (14)
- explicit classes export HOT 2
- Bind methods problem HOT 5
- Abstract Class Support HOT 2
- Documentation update
- Comparing polytype with other methods
- Using generic in baseclass HOT 17
- Improving typing for getPrototypeListOf HOT 1
- Method override does not work correctly with "crocks" library HOT 2
- Issue with Angular module HOT 3
- Issue with Angular 8 in prod mode HOT 3
- polytype extended class properties not behaving as typescript extended class propeties HOT 2
- empty catch block - not accepted by some linters HOT 2
- core.js:4117 ERROR TypeError: Constructor cannot be invoked without 'new' HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from polytype.