Angular 角度混合:TypeError:无法读取属性';获取';未定义的

Angular 角度混合:TypeError:无法读取属性';获取';未定义的,angular,dependency-injection,ng-upgrade,Angular,Dependency Injection,Ng Upgrade,我有一个NgUpgrade应用程序,我正在从Angular.js的UI路由器迁移到Angular的路由器,一切都很顺利,直到我引入了一个延迟加载的模块。然后,在惰性加载模块的一个组件中,我得到了以下结果: Found an error while navigating to /my-lazy-feature error: TypeError: Cannot read property 'get' of undefined at new MyComponent (my.component.

我有一个NgUpgrade应用程序,我正在从Angular.js的UI路由器迁移到Angular的路由器,一切都很顺利,直到我引入了一个延迟加载的模块。然后,在惰性加载模块的一个组件中,我得到了以下结果:

Found an error while navigating to /my-lazy-feature error: TypeError: Cannot read property 'get' of undefined
    at new MyComponent (my.component.ts:86)
    at NodeInjectorFactory.MyComponent_Factory [as factory] (my.component.ts:147)
    at getNodeInjectable (core.js:3503)
    at instantiateAllDirectives (core.js:9988)
    at createDirectivesInstances (core.js:9337)
    at Module.ɵɵelementStart (core.js:14559)
    at MyOtherComponent_Template (my-other.component.ts:21)
    at executeTemplate (core.js:9310)
    at renderView (core.js:9117)
    at renderComponent (core.js:10392)
    at renderChildComponents (core.js:8982)
    at renderView (core.js:9142)
    at ComponentFactory$1.create (core.js:24837)
    at ViewContainerRef.createComponent (core.js:22876)
    at RouterOutlet.activateWith (router.js:5182)
此错误是在表达式
upgrade.$injector.get
中生成的,在该表达式中,我尝试获取对Angular.js服务之一的引用:

@Component(...)
export class MyComponent implements OnInit {
    constructor(upgrade: UpgradeModule) {
        this.state = upgrade.$injector.get("$state");
        this.stateParams = upgrade.$injector.get("$stateParams");
    }
MyComponent
内部,我执行
console.log(升级)
并且我看到没有升级。$injector,但是如果我对非延迟加载的模块执行相同操作,我看到
$injector
定义为ok


因此,似乎有两个升级模块的实例。原因可能是什么?

问题最终是,延迟加载的模块正在导入一个正在导入
升级模块的模块,从而创建第二个实例

要修复此问题,请确保仅在应用程序的主模块中导入
UpgradeModule
。即使您的其他模块需要访问升级模块,也只需导入一次