简体   繁体   English

Angular 2 No Provider或DI错误(未捕获(承诺):错误:No Provider…)

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

I am getting the following exception in angular 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]

The issue is related to angular 2 DI setup via the provider is unable to resolve the SettingsService that is being used in an Authentication Guard class. 该问题与通过提供程序无法解析Authentication Guard类中使用的SettingsService的angular 2 DI设置有关。

AppModule has the following setup in its provider 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]
....

And the Authentication Guard has the following 并且Authentication Guard具有以下内容

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

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

canActivate(route: ActivatedRouteSnapshot) {
...

The error is occurring when AppModule is trying to create AuthenticationGuard and is unable to resolve or create SettingsService via DI. 当AppModule尝试创建AuthenticationGuard且无法通过DI解析或创建SettingsService时,发生错误。 Is there a better way to setup the DI or better way to debug this issue? 有没有更好的方法来设置DI或更好的方法来调试此问题?

Remove the unwanted constructor parameter from below code 从下面的代码中删除不需要的构造函数参数

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular2“t 没有提供者!” 和未捕获(承诺):错误:DI 错误 - Angular2 "No provider for t!" and Uncaught (in promise): Error: DI Error 未捕获(承诺):错误:没有 GooglePlus 的提供者 - Uncaught (in promise): Error: No provider for GooglePlus 未捕获(在承诺中):错误:没有String的提供者 - Uncaught (in promise): Error: No provider for String 未发现(承诺中):错误:没有凭据提供者? - Uncaught (in promise): Error: No provider for Credentials? 使用Karma的Angular测试中的错误:未捕获(承诺):错误:没有SessionUtil的提供程序 - Error in Angular Test using Karma: Uncaught (in promise): Error: No provider for SessionUtil 错误:未捕获(承诺):错误:没有服务提供程序-Angular 4.1.2 - Error: Uncaught (in promise): Error: No provider for Service - Angular 4.1.2 angular 4错误:未捕获(承诺):错误:没有ConnectionBackend的提供程序! 在注入Jsonp时 - angular 4 Error: Uncaught (in promise): Error: No provider for ConnectionBackend! while injecting Jsonp 错误错误:未捕获(承诺中):错误:ActivatedRoute没有提供程序 - ERROR Error: Uncaught (in promise): Error: No provider for ActivatedRoute 错误错误:未捕获(承诺):错误:没有布尔值的提供者 - ERROR Error: Uncaught (in promise): Error: No provider for Boolean '错误:未捕获(承诺):没有Jsonp的提供者'(HTML / Javascript / Typescript / Angular2) - 'Error: Uncaught (in promise): No provider for Jsonp' (HTML / Javascript / Typescript / Angular2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM