2

我在角度 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]

该问题与通过提供程序的 Angular 2 DI 设置有关,无法解析在 Authentication Guard 类中使用的 SettingsService。

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 或更好的方法来调试这个问题?

4

1 回答 1

0

从下面的代码中删除不需要的构造函数参数

constructor(
private router: Router,
private kcService: KCService,
private http: Http,
private settingsService: SettingsService
) {
}
于 2018-07-05T03:54:49.493 回答