Search code examples
angularaotangular-aot

AOT - ngc Can't resolve all parameters for Component


When I build with following JIT instructions ng build ; ng build --prod everything is ok also when I build for AOT using --aot flag, ng build --aot The app works. But when I try to compile it (AOT) using ngc I am getting below error:

Can't resolve all parameters for HomeComponent in /appname/src/app/home/home.component.ts: (?).

This is the HomeComponent class:

import {EventManager} from 'app/directives/EventManager.directive';
@Component({
  selector:'home',
  template:`
          ...
    `,
  styleUrls: ['./home.component.css']
})
    export class HomeComponent  {

      showLoggedBar:Boolean;
      constructor(private _eventManager:EventManager) {

        this._eventManager.showLoggedBar.subscribe((mode)=> {
          if(mode)
          {
            this._eventManager.showBar.emit(true);
            this.showLoggedBar = mode;
          }
        });
      }
    }

[EDITED] EventManager :

@Injectable()
export class EventManager {
    public showLoggedBar: EventEmitter<any> = new EventEmitter();
    public showLoggedDoBar: EventEmitter<any> = new EventEmitter();
    public showDoBar: EventEmitter<any> = new EventEmitter();
    public showBar:EventEmitter<any>=new EventEmitter();
    public dataSearch:EventEmitter<any>= new EventEmitter();
    public updateP:EventEmitter<any>=new EventEmitter();
    public updateD:EventEmitter<any>=new EventEmitter();
    public detailsAvailable:EventEmitter<any>= new EventEmitter();
    public infoAp:EventEmitter<any>= new EventEmitter();

  constructor() {
        this.showBar.emit(true);
    }
}

[EDITED] app.module.ts :

import{NgModule} from '@angular/core';
import {
  LocationStrategy,
  PathLocationStrategy
} from '@angular/common';
import { BrowserModule  } from '@angular/platform-browser';
import { AppComponent }   from './app.component';
import { routing,  appRoutingProviders } from './app.routing';
import { HomeComponent } from './home/home.component';
import { HttpModule } from '@angular/http';
import {EventManager} from '../app/directives/EventManager.directive';


@NgModule({
  declarations:
    [
      ...
      HomeComponent,
      ...
    ],
  imports:      [BrowserModule,HttpModule,...],
  bootstrap:    [AppComponent],
  providers:[
    ...
    EventManager,
    ...
  ]
})
export class AppModule {}

Do you have any idea of reason for the error ?


Solution

  • When you inject EventManager, then you need to provide EventManager. Providing GlobalEventManager is meaningless.

    To inject a GlobalEventManager instance, when an EventManager is requested, use useClass:

    providers: [{ provide: EventManager, useClass: GlobalEventManager }]
    

    if there are components or services that inject GlobalEventManager, you can use useExisting to avoid two different instances being created:

    providers: [
        GlobalEventManager, 
        { provide: EventManager, useExisting: GlobalEventManager}
    ]