angular: Providing from inheritor to constructor of base component doesn't work in Angular 4

I’m submitting a … (check one with “x”)

[X] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

After updating to Angular 4 code below throws errors (Cannot read property ‘instance’ of undefined). It was working without any problems before update:

export` const entity = new InjectionToken<string>('entity');

export class BaseComponent {
   protected entity<string>;
   constructor(injector: Injector) {
       ...
       this.entity = injector.get(entity)
   }
}

@Component({
   ...
   providers: [{provide: entity, useValue: 'whoCares'}]
})
export class InheritorComponent {
    constructor(injector: Injector) {
        super(injector)
    }
}

Expected behavior

I excepted InheritorComponent to be initialized with provided data via token.

Minimal reproduction of the problem with instructions

Here is working example: http://plnkr.co/edit/CQLX3D5WWjixnzV5zQgN?p=preview Here is unwoking (to brake it uncomment 26 line of app.ts) http://plnkr.co/edit/5yeUn0j1VxASsAD0657n?p=preview

Code is similar. The only difference is angular version.

Please tell us about your environment:

  • Angular version: 4.0.X
  • Browser: [all ]
  • Language: [TypeScript 2.2]

  • Node (for AoT issues): node --version = 6.9.2

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 4
  • Comments: 16 (4 by maintainers)

Most upvoted comments

Doesn’t work either with useClass. I have encountered this issue on upgrade to Angular 4.