Angularjs 为什么在使用Upgrade组件时$injector是空的
我尝试通过引导混合ng1和ng2从angular1升级到angular2 代码如下: 当我运行这个示例时,我得到一个错误,它说:Angularjs 为什么在使用Upgrade组件时$injector是空的,angularjs,angular,Angularjs,Angular,我尝试通过引导混合ng1和ng2从angular1升级到angular2 代码如下: 当我运行这个示例时,我得到一个错误,它说: Cannot read property 'get' of undefined at HeroDetailDirective.UpgradeComponent (https://unpkg.com/@angular/upgrade/bundles/upgrade-static.umd.js:512:43) at new HeroDetailDirective (ht
Cannot read property 'get' of undefined
at HeroDetailDirective.UpgradeComponent (https://unpkg.com/@angular/upgrade/bundles/upgrade-static.umd.js:512:43)
at new HeroDetailDirective (https://run.plnkr.co/QOdaB9QdPbHLfQe9/app/main.0.ts!transpiled:45:16)
at new Wrapper_HeroDetailDirective (/AppModule/HeroDetailDirective/wrapper.ngfactory.js:7:18)
调试时,我看到Upgrade组件中的$injector为空:
function UpgradeComponent(name, elementRef, injector) {
this.name = name;
this.elementRef = elementRef;
this.injector = injector;
this.controllerInstance = null;
this.bindingDestination = null;
this.$injector = injector.get($INJECTOR);
你知道为什么吗
谢谢您的帮助。找到了问题
我定义了引导AppComponent的步骤:
@NgModule({
imports: [ BrowserModule, UpgradeModule ],
declarations: [ AppComponent, HeroDetailDirective ],
bootstrap: [ AppComponent ]
})
class AppModule {
ngDoBootstrap() {}
}
当我删除引导定义时,它起了作用:
@NgModule({
imports: [ BrowserModule, UpgradeModule ],
declarations: [ AppComponent, HeroDetailDirective ]
})
class AppModule {
ngDoBootstrap() {}
}
我没有意识到混合应用程序总是由ng1引导。这篇文章对我帮助很大:什么是未定义的<代码>此。$injector或
$injector
此。$injector未定义执行此操作的对象。injector
从this.injector=injector
?元素injector。我猜是这样的:实际的问题是为什么这个例子不起作用。我错过什么了吗?