question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Injection issue with InjectionToken

See original GitHub issue

I’m submitting a…


[ x] Bug report 

Current behavior

When I run this example, I get an exception in the console

TypeError: Cannot read property 'ngInjectableDef' of undefined
    at resolveNgModuleDep (vendor.js:39470)
    at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (vendor.js:40174)
    at inject (vendor.js:32520)
    at injectArgs (vendor.js:32551)
    at vendor.js:32608
    at _callFactory (vendor.js:39548)
    at _createProviderInstance (vendor.js:39506)
    at resolveNgModuleDep (vendor.js:39481)
    at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (vendor.js:40174)
    at resolveDep (vendor.js:40539)

Expected behavior

No exception should occur and injection should be made

Minimal reproduction of the problem with instructions

@Injectable({
  providedIn: 'root'
})
export class AppService {

  constructor(@Inject(APP_CONFIG) private config:Config) {
  }
}

export const APP_CONFIG = new InjectionToken<Config>
('app.config', {
  providedIn: 'root',
  factory: () => AppConfig
});

export interface Config {
  apiUrl: string;
}

export const AppConfig: Config = {
  apiUrl: 'http://localhost:8080/api/'
};

Environment


Angular version: 6.1.8
Browser:
- [x ] Chrome (desktop) version 69.0
 

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
sergey-morenetscommented, Feb 10, 2019

Verified this issue on Angular 7.2.4. I still got an error but the error message is different:

TypeError: Cannot read property '__source' of undefined
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:8984)
    at resolveNgModuleDep (core.js:21119)
    at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (core.js:21808)
    at injectInjectorOnly (
1reaction
sergey-morenetscommented, Jan 27, 2020

Not reproduced any more with Angular9-rc.9

Read more comments on GitHub >

github_iconTop Results From Across the Web

InjectionToken - Angular
Use an InjectionToken whenever the type you are injecting is not reified (does not have a runtime representation) such as when injecting an...
Read more >
Angular Injection Token use cases - Country Code - Ghost
An Angular Injection Token is no different. You create an Injection Token by calling new InjectionToken('some description string'), this returns ...
Read more >
Configure your Angular apps with an injection token - Medium
Players of dependency injection in Angular: provider, injector and requesting component. Today we are going to have a look at a practical use...
Read more >
Injection Token in Angular - TekTutorialsHub
The Injection Token allows creating token that allows the injection of values that don't have a runtime representation. It is very similar to ......
Read more >
Inject() for InjectionToken declared in module fails in angular2
Recently I've run into problem with InjectionToken, that is declared in module import {InjectionToken, NgModule} from '@angular/core'; ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found