【问题标题】:AOT - ngc Can't resolve all parameters for ComponentAOT - ngc 无法解析组件的所有参数
【发布时间】:2017-07-27 20:24:45
【问题描述】:

当我按照 JIT 指令 ng build 构建时; ng build --prod 一切正常,当我使用 --aot 标志为 AOT 构建时,ng build --aot 该应用程序有效。 但是当我尝试使用ngc 编译它(AOT)时,我遇到了以下错误:

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

这是 HomeComponent 类:

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;
          }
        });
      }
    }

[编辑] 事件管理器:

@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);
    }
}

[编辑] 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 {}

你知道错误的原因吗?

【问题讨论】:

  • 你在哪里导入/提供eventManager
  • 我想问题应该是在哪里提供的。
  • @GünterZöchbauer “提供的地方”是什么意思?我用 EventManager 指令更新了我的问题
  • 嗯...EventsManager, EventManager, GlobalEventsManager 你到底在用什么?
  • 如果你想注入一些东西,它需要在某个地方提供@NgModule({ ..., providers: [{provide: EventManager, useClass: GlobalEventManager}], ... }) export class AppModule {}

标签: angular aot angular-aot


【解决方案1】:

注入EventManager时,需要提供EventManager。提供GlobalEventManager 毫无意义。

要注入GlobalEventManager 实例,当请求EventManager 时,使用useClass

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

如果有注入GlobalEventManager的组件或服务,可以使用useExisting来避免创建两个不同的实例:

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

【讨论】:

  • 我更新了app.module.ts 文件,如我的问题所示,但结果仍然相同
  • 很难说。你能在 Plunker 中复制吗?
  • 将服务存储在directives/Foo.directive 文件中是否有原因?
  • 这是一个错误,它的位置在services文件夹中
猜你喜欢
  • 2017-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-24
  • 1970-01-01
  • 2020-04-29
  • 2018-05-18
相关资源
最近更新 更多