我建立了一個角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