我建立了一个角Module
有它自己的服务和组件:Angular2没有提供服务 - 外部模块
@NgModule({
declarations: [ FileUploader],
exports: [ FileUploader ],
imports: [ CommonModule ],
providers: [ FileService ],
})
export class FilesModule { }
而且FileService
:
import { Injectable } from '@angular/core';
@Injectable()
export class FileService
{
constructor() {}
uploadFile(file): Promise {
return new Promise((resolve, reject) => {
...
}
}
}
然后我将它导入到我的AppModule
:
@NgModule({
declarations: [ AppComponent ],
entryComponents: [ AppComponent ],
imports: [ FilesModule ]
})
export class AppModule { }
当我注入FileService
到AppComponent
我得到一个错误:
错误AppComponent:无提供的FileService
从角文档:
When we import a module, Angular adds the module's service providers (the contents of its providers list) to the application root injector.
This makes the provider visible to every class in the application that knows the provider's lookup token.
我缺少什么,使这项工作?
您是否在您的app.module.ts中导入FileService? – Pterrat