Solución:
Para TypeScript y Angular 5 de hoy, evite WARNING in Circular dependency detected
al importar el inyector global, primero declare un ayudante, por ejemplo app-injector.ts
:
import {Injector} from '@angular/core';
/**
* Allows for retrieving singletons using `AppInjector.get(MyService)` (whereas
* `ReflectiveInjector.resolveAndCreate(MyService)` would create a new instance
* of the service).
*/
export let AppInjector: Injector;
/**
* Helper to set the exported {@link AppInjector}, needed as ES6 modules export
* immutable bindings (see http://2ality.com/2015/07/es6-module-exports.html) for
* which trying to make changes after using `import {AppInjector}` would throw:
* "TS2539: Cannot assign to 'AppInjector' because it is not a variable".
*/
export function setAppInjector(injector: Injector) {
if (AppInjector) {
// Should not happen
console.error('Programming error: AppInjector was already set');
}
else {
AppInjector = injector;
}
}
A continuación, en tu AppModule
, configúrelo usando:
import {Injector} from '@angular/core';
import {setAppInjector} from './app-injector';
export class AppModule {
constructor(injector: Injector) {
setAppInjector(injector);
}
}
Y donde sea necesario, use:
import {AppInjector} from './app-injector';
const myService = AppInjector.get(MyService);
Me las arreglé para hacerlo usando boostrapping manual. No use “bootstrap: [AppComponent]
“declaración en @NgModule
, usar ngDoBootstrap
método en su lugar:
export class AppModule {
constructor(private injector: Injector) {
}
ngDoBootstrap(applicationRef: ApplicationRef) {
ServiceLocator.injector = this.injector;
applicationRef.bootstrap(AppComponent);
}
}
Otra solución con angular 2.0.0 final:
platformBrowserDynamic().bootstrapModule(AppModule, [
{
defaultEncapsulation: ViewEncapsulation.Emulated,
providers: [
{ provide: TRANSLATIONS, useValue: TRANSLATION },
{ provide: TRANSLATIONS_FORMAT, useValue: 'xlf' },
{ provide: LOCALE_ID, useValue: 'fr' }
]
}
]).then((modref: NgModuleRef<any>) => {
appInjector(modref.injector);
});
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)