簡體   English   中英

Angular 2 No Provider或DI錯誤(未捕獲(承諾):錯誤:No Provider…)

[英]Angular 2 No Provider or DI error (Uncaught (in promise): Error: No provider …)

我在角度2中收到以下異常。

EXCEPTION: Uncaught (in promise): Error: No provider for SettingsService!
Error: DI Error
    at NoProviderError.BaseError [as constructor] (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:1244:31) [angular]
    at NoProviderError.AbstractProviderError [as constructor] (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:1365:20) [angular]
    at new NoProviderError (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:1405:20) [angular]
    at ReflectiveInjector_._throwOrNull (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:2937:23) [angular]
    at ReflectiveInjector_._getByKeyDefault (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:2976:29) [angular]
    at ReflectiveInjector_._getByKey (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:2908:29) [angular]
    at ReflectiveInjector_.get (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:2777:25) [angular]
    at AppModuleInjector.get (/AppModule/module.ngfactory.js:483:165) [angular]
    at AppModuleInjector.getInternal (/AppModule/module.ngfactory.js:621:49) [angular]
    at AppModuleInjector.NgModuleInjector.get (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:8490:48) [angular]
    at PreActivation.getToken (http://localhost:5555/node_modules/@angular/router/bundles/router.umd.js:4160:29) [angular]
    at MapSubscriber.eval [as project] (http://localhost:5555/node_modules/@angular/router/bundles/router.umd.js:4056:52) [angular]
    at MapSubscriber._next (http://localhost:5555/node_modules/.tmp/Rx.min.js?1488555021058:11:17343) [angular]
    at MapSubscriber.Subscriber.next (http://localhost:5555/node_modules/.tmp/Rx.min.js?1488555021058:11:13507) [angular]

該問題與通過提供程序無法解析Authentication Guard類中使用的SettingsService的angular 2 DI設置有關。

AppModule在其提供程序中具有以下設置

...
providers: [
Service1,
Service2,
KCService,
SettingsService,
AuthenticationGuard,
{
  provide: Http,
  useFactory:
  (backend: XHRBackend,
    defaultOptions: RequestOptions,
    kcService:KCService) => new KCHttp(backend, defaultOptions, kcService),
  deps: [XHRBackend, RequestOptions, KCService]
},
{
  provide: APP_BASE_HREF,
  useValue: '<%= APP_BASE %>'
}],
bootstrap: [AppComponent]
....

並且Authentication Guard具有以下內容

...
@Injectable()
export class AuthenticationGuard implements CanActivate {

constructor(
private router: Router,
private kcService: KCService,
private http: Http,
private settingsService: SettingsService
) {
}

canActivate(route: ActivatedRouteSnapshot) {
...

當AppModule嘗試創建AuthenticationGuard且無法通過DI解析或創建SettingsService時,發生錯誤。 有沒有更好的方法來設置DI或更好的方法來調試此問題?

從下面的代碼中刪除不需要的構造函數參數

constructor(
private router: Router,
private kcService: KCService,
private http: Http,
private settingsService: SettingsService
) {
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM