我正在使用angular的$ injector动态加载服务并调用其函数:
const actionService = this.$injector.get(action); //in this case action = displayScreenTaskActionService
actionService.initTaskAction();
注入的服务类别:
// imports ...
export class DisplayScreenTaskActionService {
constructor(
protected $rootScope: IRootScopeService,
protected localStorageService: ILocalStorageService,
protected screenService: ScreenService,
protected notifyService: NotifyService
) { }
initTaskAction() {
this.screenService.loadScreens({
limit: 0,
orderBy: 'name'
})
...
}
}
export default angular.module('taskActions.displayScreen',[localStorageService, screenService, notifyService])
.service('displayScreenTaskActionService', DisplayScreenTaskActionService).name;
这行得通,但是当注入的类扩展了某个类
export class DisplayScreenTaskActionService extends GenericTaskActionService {
然后DisplayScreenTaskActionService
构造函数中的依赖项将不会被注入,并且是未定义的。当我使用不带$ injector的服务时,依赖项将被加载,即使它扩展了某些类。
在项目中,使用1.7.5字体和3.1.1打字稿