当我运行
ng serve
时,应用程序成功编译且没有错误。 但是,在运行时,我在控制台上收到以下消息:Error: Trying to get the AngularJS injector before it being set.
什么原因导致了这个错误,我该如何避免它?我没有偏离升级指南中详细说明的步骤。
以下是我在Angular应用程序中升级AngularJS组件的方式:
// example.directive.ts
import { Directive, ElementRef, Injector } from '@angular/core';
import { UpgradeComponent } from '@angular/upgrade/static';
// this is the npm module that contains the AngularJS component
import { MyComponent } from '@my-company/module-test';
@Directive({
selector: 'my-upgraded-component'
})
export class ExampleDirective extends UpgradeComponent {
constructor(elementRef: ElementRef, injector: Injector) {
// the .injectionName property is the component's selector
// string; "my-component" in this case.
super(MyComponent.injectionName, elementRef, injector);
}
}
这是我的 app.module.ts
文件:
// app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { UpgradeModule } from '@angular/upgrade/static';
import { ExampleDirective } from './example.directive';
import { myModuleName } from '@my-company/module-test';
@NgModule({
declarations: [AppComponent, ExampleDirective],
imports: [BrowserModule, AppRoutingModule, UpgradeModule],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
constructor(private upgrade: UpgradeModule) {}
ngDoBootstrap() {
this.upgrade.bootstrap(document.body, [myModuleName], {
strictDi: true
});
}
}
我正在使用Angular 5.2.0。
@my-company/module-test
时,为什么每个文件的导入语句都不同?而且在第一个文件中似乎没有对导入的MyComponent
进行任何操作。 - Pop-A-Stash@my-company/module-test
导入不同的内容,因为该模块导出多个命名导出。 - Nathan FriendUpgradeModule
时,我遇到了这个错误。一旦我将其移除,错误就消失了。 - Carlo Bos