错误:无法解析myComponent的
问题描述:
所有参数我得到这个问题错误:无法解析myComponent的
Error: Can't resolve all parameters for myComponent
,我发现原因是由于特定服务的循环依赖。
我有common.service.ts为延伸从BaseService如下,
@Injectable()
export class CommonService extends BaseService {
constructor(
private _http: Http,
private _appConfig: AppConfigService
) {
super(_http, _appConfig);
}
和BaseService.ts
@Injectable()
export class BaseService {
constructor(private http: Http, @Inject(AppConfigService) private appConfig: AppConfigService) {
}
get(url: string): any {
return this.http.get(`${this.appConfig.getAPIUrl()}${url}`)
.map(this.extractData)
.catch(this.handleErrors);
}
}
代码明确误差是由于正在注入的事实http和appconfig服务两次在commonservice中,另一个在基本服务中。
在这种情况下,我该如何避免循环依赖?
答
尽量不注射任何东西到你的基类的构造函数,而是只注入你的依赖关系派生类,然后将它们传递给super()
像代码:
export class BaseService {
constructor(http: Http, appConfig: AppConfigService) {
}
get(url: string): any {
return this.http.get(`${this.appConfig.getAPIUrl()}${url}`)
.map(this.extractData)
.catch(this.handleErrors);
}
}
@Injectable()
export class CommonService extends BaseService {
constructor(
private _http: Http,
private _appConfig: AppConfigService
) {
super(_http, _appConfig);
}
+0
多数民众赞成在现在是如何,你可以提供代码! –
+0
你在两个类中都使用了@ @ Injectable:基类和派生类,所以不,现在不是这样。 – classicalConditioning
尝试使用'@Injectable()'注释在你的'BaseService'类 – echonax
@echonax我没有得到它 –
就像你在'CommonService'中使用它。在类的顶部写上'@Injectable()' – echonax