Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upOnInit is not respecting inheritance #6781
Comments
|
If B doesn't have (override) |
|
This is the setup correct? import {bootstrap} from 'angular2/platform/browser';
import {Component, OnInit} from 'angular2/core';
class B {
ngOnInit() {
console.log('B ngOnInit');
}
}
@Component({ selector: 'a-b', template: 'AB' })
class A extends B implements OnInit {
constructor() {
super();
}
ngOnInit() {
super.ngOnInit(); // call B's ngOnInit
console.log('A ngOnInit');
}
}
bootstrap(A); |
|
@gdi2290 > but B does not explicitly declare that it implements OnInit, only that it extends A Ok, so you mean this only refers to @trinarytree can you please confirm? |
|
i can't reproduce this at all. please provide a plunker reproduction? See https://plnkr.co/edit/f3kcWBPF64IRCGUmUKPF?p=preview for a functioning version. Note that |
|
I'm amazed at how many comments this got between fri 22:52 and sat. I don't normally work so much on saturday morning. :-)
No. A already implements ngOnInit, and in my case B is fine with inheriting A's version.
No. It looks like this (in Dart): abstract class A implements OnInit {
@override
void ngOnInit() {
print('A#ngOnInit'); // This never gets called, but should. Hence the bug report.
}
}
@Component(
selector: 'b',
template: 'angular should but does not call my ngOnInit'
)
class B extends A {}I suspect that this bug only happens when Angular compiles Dart code - it might not show up in TypeScript. Imagine what would happen if the code transformer used reflection incorrectly, asking whether B explicitly declared that it implements OnInit instead of asking whether it's a subclass. |
|
Could you please try to add |
|
having the same issue here, giving the advice of @zoechi a chance now |
|
for me the following combination works in beta2
Also when I remove |
|
If you add When I search the Angular2 code, I can't find any references to |
|
This is the intended behavior of Dart's compiled output - you must explicitly declare that This was a design decision to keep the build process fast. Supporting this would mean that changes to an arbitrary file |
|
I got a recommendation to add a dev-mode-only check to Spun off #6849 to track. |
|
That's correct, #4222 will be reverted and I will add some better comments & documentation explaining why things work the way they do. https://goo.gl/b07Kii is one initial piece, and I'm in contact with the folks who are writing the Dart docs on angular.io to ensure that this behavior is well documented. |
|
@kegluneq that's real sad. That makes inheritance and mixins pretty much useless. |
|
I agree that it's sad I disagree that it makes inheritance & mixins useless. They do require additional boilerplate to function the way you expect, and this does violate encapsulation, but they can still be used effectively. |
|
I just run into this one too (in Dart). @kegluneq Please reconsider this: I would say that your "intended bahavior" violates basic OOP contracts :-/ If "A extends B" and "B is OnInit" then "A is OnInit" too. Real world example: Imagine backend/admin application, with global menu and big area for main content (Google Cloud Console, for example). I would create "BaseContentComponent" and all main screens would inherit from it: "MessagesContentComponent extends BaseContentComponent". BaseContentCompoment declares what is "a content" - it has a headline, it has a sub-menu and probably it has some lifecycle callback, let's say BaseContentComponent implements OnInit to do some generic stuff. Now I have to declare "MessagesContentComponent extends BaseContentComponent implements OnInit" ... I don't like it, but I can do it. Now let's say I create 50 content components like this. And then I decide to add new lifecycle callback to BaseContentComponent - for example OnDestroy I want to put each visited screen into "history/shortcuts" panel. Now I have to go through all 50 content components to add OnDestroy? Noooooooooooo ... |
|
@kegluneq Great, thanks! |
|
Let's centralize discussion on inheritance in #11606. Closing as a duplicate. |
|
The master issue is closed Is this now fixed for AOT? Or is there another issue tracking OnInit in case of AOT? |
|
I also run into this, with typescript 3.3.3. The ngOnInit of subclass won't get called if I omit a declaration of ngOnInit. So in every subclass I have to add
This is pretty rediculous. |
|
is the minification to blame? |
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |

Suppose you have this class hierarchy
A
|
B
and A implements OnInit but B does not explicitly declare that it implements OnInit, only that it extends A. Then angular will (incorrectly) not call ngOnInit when it creates an instance of B.