2
我正在尝试创建一个小框架,以使我更容易实现CRUD功能。Angular 2将服务注入到另一个服务中是未定义的
所以我创建了这样一个BaseHttpService:
... <imports> ...
@Injectable()
export class BaseHttpService {
constructor(
private http: Http
) {
console.log('ctor');
this.apiUrl$.subscribe(apiUrl => this.rootApiUrl = apiUrl.removeTrailingSlash());
}
public getPagedResultOfType<T extends IBaseEntity>(url: string, vm: ISearchVm<T>, options?: RequestOptions): Observable<PagedResult<T>>{ ... <implementation> ... }
... <other methods> ...
}
然后,我有我的BaseEntityReadOnlyService使用此BaseHttpService创建CRUD的阅读fucntionality:
@Injectable()
export class BaseAppSumEntityReadOnlyService<T extends IBaseEntity>{
constructor(
@Inject(forwardRef(() => BaseHttpService)) public baseHttpService: BaseHttpService
// public baseHttpService: BaseHttpService
) {
console.log('ro ctor');
console.log(this.baseHttpService); // <-- this is always undefined
}
getAll(vm: ISearchVm<T>): Observable<PagedResult<T>>{
const url = `${this.baseUrl}`;
console.log(this.baseHttpService);
return this.baseHttpService.getPagedResultOfType<T>(url, vm); // <-- this fails because baseHttpService is undefined: Cannot read property 'getPagedResultOfType' of undefined
}
... <more methods>
}
在我FrameworkModule,我试过很多:
@NgModule({
declarations: [
...
],
imports: [
CommonModule,
HttpModule
],
providers: [
BaseHttpService,
// BaseAppSumEntityReadOnlyService
{ provide: BaseAppSumEntityReadOnlyService, useClass: BaseAppSumEntityReadOnlyService, deps: [ BaseHttpService ] },
]
})
export class AppSumFrameworkModule { }
这就是我目前有,但这是行不通的。我得到的错误是:
无法读取的不确定
财产“getPagedResultOfType”我怎样才能得到BaseHttpService在我BaseAppSumEntityReadOnlyService注入?
这是它是如何完成当前:从 './base-http.service' 进口{BaseHttpService}; @Injectable() export class BaseAppSumEntityReadOnlyService {构造函数( protected baseHttpService:BaseHttpService ){ console.log('ro ctor - '); console.log(this.baseHttpService); }} //控制台日志给出:'ro ctor - 'undefined –
AppSum
更改第二个''protected''为'private'并重新编译App。看看问题是否依然存在。我会继续研究你的问题,看看我是否发现问题的地方 – SrAxi
@AppSum在你的示例代码中,我看到了这个@Inject(forwardRef(()=> BaseHttpService))public baseHttpService:BaseHttpService'。那么你正在使用哪个版本?您的OP上的还是此评论上的? – SrAxi