问题
Ok, sorry if the title was not clear.
I'm trying to implement a pattern involving injecting interfaces. It works perfectly in JIT but not in AoT. I was hoping to get an explanation of why this doesn't work and hopefully some suggestions to make it work. Here's a simplified example of the pattern.
data-manager.interface.ts
@Injectable() export class DataManager implements DataManager {}
export interface DataManager {
getData(): Promise<Array>;
...
}
^Declares Interface
dashboard.service.ts
@Injectable() export class DashboardService {
constructor(public dataManager: DataManager, ... ){
...
}
}
^Injects Interface
app.service.ts
import { DataManager as DashboardDataManager } from '.../data-manager.interface';
@Injectable() export class AppService implements DashboardDataManager {
getData(): Promise<Array>{
...
}
}
^Implements Interface
app.modules.ts
import { DataManager as DashboardDataManager } from '.../data-manager.interface';
@NgModule({
...,
providers: [
AppService,
{ provide: DashboardDataManager, useExisting: AppService },
...
]
})
^Provides Interface
This pattern tricks the compiler (the JIT compiler at least) into allowing a DataManager to be injected. But why does it work for JIT and not AOT?
Here is the error that I am getting from the compiler:
ERROR in Can't resolve all parameters for DashboardService in dashboard.service.ts: (?, [object Object], [object Object], [object Object]).
*note that the compiler prints out the full path but I edited it out and only left the file name.
Please help! I would really appreciate a clean solution to this!
Thanks, Ethan
来源:https://stackoverflow.com/questions/47163715/why-does-angular-project-fail-aot-build-when-injecting-interfaces