AOT - ngc 无法解析组件的所有参数
AOT - ngc Can't resolve all parameters for Component
当我使用以下 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 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;
}
});
}
}
[已编辑] 事件管理器:
@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
时,需要提供EventManager
。提供 GlobalEventManager
是没有意义的。
要注入 GlobalEventManager
实例,当请求 EventManager
时,使用 useClass
:
providers: [{ provide: EventManager, useClass: GlobalEventManager }]
如果有组件或服务注入 GlobalEventManager
,您可以使用 useExisting
来避免创建两个不同的实例:
providers: [
GlobalEventManager,
{ provide: EventManager, useExisting: GlobalEventManager}
]
当我使用以下 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 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;
}
});
}
}
[已编辑] 事件管理器:
@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
时,需要提供EventManager
。提供 GlobalEventManager
是没有意义的。
要注入 GlobalEventManager
实例,当请求 EventManager
时,使用 useClass
:
providers: [{ provide: EventManager, useClass: GlobalEventManager }]
如果有组件或服务注入 GlobalEventManager
,您可以使用 useExisting
来避免创建两个不同的实例:
providers: [
GlobalEventManager,
{ provide: EventManager, useExisting: GlobalEventManager}
]