Why does angular project fail AoT build when injecting Interfaces?

江枫思渺然 提交于 2020-01-24 21:55:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!