我需要有关如何将服务注入到我拥有的另一服务中的帮助。设置如下:
这是我的bootstrapModule所在的main.ts:
platformBrowserDynamic()
.bootstrapModule(DTIframeModule, {
preserveWhitespaces: false
}).then((ngModuleRef: NgModuleRef<DTIframeModule>) => {
const rootInjector = ngModuleRef.injector;
const instrumentationService = rootInjector.get(InstrumentationService);
if (instrumentationService instanceof DTService) {
const routeService = rootInjector.get(DTRouteService);
instrumentationService.registerDTRouteService(routeService);
}
}
还有InstrumentationService
和DTService
的详细信息:
@Injectable()
export abstract class InstrumentationService {
public abstract generateDTEvent(
eventName: string,
params?: Params): InstrumentationEvent;
}
@Injectable()
export class DTService implements InstrumentationService {
private queuedEventsPopulatedWithRoute: boolean;
// few other fields
public registerRouteService(routeService: RouteService) {
// some function
}
public generateDTEvent(
eventName: string,
params?: Params): InstrumentationEvent {
// some service
}
}
我遇到的问题是我想在WinService
内使用名为DTService
的服务,我添加了导入:
import { WinService } from '../services/win.service';
并想像这样在DTService
内部访问它:
this.winService.native.events;
但是,但是我试着撞墙,并收到错误消息this.winService未定义。我尝试将构造函数添加到DTService
并添加private readonly winService: WinService
,这会引发注入错误。
任何帮助将不胜感激。谢谢!
答案 0 :(得分:1)
请在模块的提供者数组中添加WinService
。
如果在另一个模块中提供了该模块,请将该模块导入您提供了DTService
请阅读Angular Dependency Injection here了解更多信息。