Dependency injection 将定制服务注入到定制服务中
使用Angular2 RC1 假设我有两个自定义服务:例如ConfigService和AuthService 并且还假设AuthService需要来自ConfigService的数据 配置服务就是这样Dependency injection 将定制服务注入到定制服务中,dependency-injection,angular,Dependency Injection,Angular,使用Angular2 RC1 假设我有两个自定义服务:例如ConfigService和AuthService 并且还假设AuthService需要来自ConfigService的数据 配置服务就是这样 import { Injectable } from '@angular/core'; @Injectable() export class ConfigService { public baseURL: string; constructor() { this.b
import { Injectable } from '@angular/core';
@Injectable()
export class ConfigService {
public baseURL: string;
constructor() {
this.baseURL = 'http://localhost:8080';
}
}
我的ConfigService必须是单例,因此我已在引导提供程序阵列中声明它:
bootstrap(MyWayAppComponent, [
HTTP_PROVIDERS,
...
ConfigService
]);
当我尝试在任何组件中注入ConfigService时没有问题,我总是获取相同的实例
但是现在我想将这个ConfigService注入我的AuthService(AuthService不是一个单例,而是在我的auth组件中提供的)
我在AuthService中注入ConfigService,如下所示:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { ConfigService } from '../services/ConfigService';
@Injectable()
export class AuthService {
constructor(private http: Http, private configService: ConfigService) {
}
}
回应如下:
EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in :0:0
ORIGINAL EXCEPTION: No provider for ConfigService!
ORIGINAL STACKTRACE:
Error: DI Exception
at NoProviderError.BaseException [as constructor] (http://localhost:4200/vendor/@angular/core/src/facade/exceptions.js:17:23)
at NoProviderError.AbstractProviderError [as constructor] (http://localhost:4200/vendor/@angular/core/src/di/reflective_exceptions.js:39:16)
at new NoProviderError (http://localhost:4200/vendor/@angular/core/src/di/reflective_exceptions.js:75:16)
at ReflectiveInjector_._throwOrNull (http://localhost:4200/vendor/@angular/core/src/di/reflective_injector.js:776:19)
at ReflectiveInjector_._getByKeyDefault (http://localhost:4200/vendor/@angular/core/src/di/reflective_injector.js:804:25)
at ReflectiveInjector_._getByKey (http://localhost:4200/vendor/@angular/core/src/di/reflective_injector.js:767:25)
at ReflectiveInjector_.get (http://localhost:4200/vendor/@angular/core/src/di/reflective_injector.js:576:21)
at ElementInjector.get (http://localhost:4200/vendor/@angular/core/src/linker/element_injector.js:23:48)
at ElementInjector.get (http://localhost:4200/vendor/@angular/core/src/linker/element_injector.js:23:48)
at ReflectiveInjector_._getByKeyDefault (http://localhost:4200/vendor/@angular/core/src/di/reflective_injector.js:801:24)
ERROR CONTEXT:
[object Object]
我仔细阅读了帕斯卡·普雷希特(Pascal Precht)的优秀文章,但我看不出我错在哪里
注意:如果我尝试注入一个“常规”服务(如http),我没有pb。。。因此,我应该忘记服务声明中的一些内容,但是什么呢?只有在引导应用程序或组件级别时才能定义提供者 因此,您可以在此级别定义ConfigService 有关更多详细信息,请参见此问题:
provide(AuthService, {
useFactory: (config) => {
return new...
},
depend: [ ConfigService ]
});
只能在引导应用程序时或在组件级别定义提供程序 因此,您可以在此级别定义ConfigService 有关更多详细信息,请参见此问题:
provide(AuthService, {
useFactory: (config) => {
return new...
},
depend: [ ConfigService ]
});
您的代码看起来不错,您是否可能有多个
ConfigService
类?。/services/ConfigService
与引导中的相同吗?Arggghh,你说得对!!!!大写/小写字母有问题!!!太多了!!!不客气,很乐意帮助。您的代码看起来不错,您是否可能有多个ConfigService
类?。/services/ConfigService
与引导中的相同吗?Arggghh,你说得对!!!!大写/小写字母有问题!!!太多了!!!不客气,很乐意帮忙。