【发布时间】:2017-07-21 22:22:53
【问题描述】:
我构建了一个 Angular Module,它拥有自己的服务和组件:
@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 的提供程序
来自 Angular 文档:
当我们导入一个模块时,Angular 会将模块的服务提供者(其提供者列表的内容)添加到应用程序根注入器。
这使得应用程序中知道提供程序查找令牌的每个类都可以看到提供程序。
我缺少什么来完成这项工作?
【问题讨论】:
-
你在 app.module.ts 中导入 FileService 吗?