错误:在执行AngularJS到Angular 13的迁移时,可注入类PlatformLocation{}的JIT编译失



在将应用程序从angularjs迁移到angular v13时,我正在尝试双重启动该应用程序。

并在浏览器控制台中得到以下错误:

Uncaught Error: The injectable 'PlatformLocation' needs to be compiled using the JIT compiler, but '@angular/compiler' is not available.
The injectable is part of a library that has been partially compiled.
However, the Angular Linker has not processed the library such that JIT compilation is used as fallback.
Ideally, the library is processed using the Angular Linker to become fully AOT compiled.
Alternatively, the JIT compiler should be loaded by bootstrapping using '@angular/platform-browser-dynamic' or '@angular/platform-server',
or manually provide the compiler with 'import "@angular/compiler";' before bootstrapping.

下面是我用来配置这个双启动过程的文件。

主.ts

//angularjs imports 
import { DoBootstrap, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { UpgradeModule } from '@angular/upgrade/static';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
@NgModule({
imports: [
BrowserModule,
UpgradeModule
]
})
export class AppModule{
// Override Angular bootstrap so it doesn't do anything
ngDoBootstrap() {}
}
// Bootstrap using the UpgradeModule
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
console.log("Bootstrapping in Hybrid mode with Angular & AngularJS");
const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
upgrade.bootstrap(document.body, ['codecraft']);
});

我不想在main.ts中执行import '@angular/compiler';,尽管这似乎暂时有效,但在以后迁移组件时会引发类似的问题。

理想情况下,我不想禁用AOT或IVY。

尝试过

  1. "postinstall":"ngcc-属性es2015浏览器模块主-仅限-创建常春藤入口点">
  2. npm更新
  3. webpack配置中的babel加载程序

您很可能缺少导入angular编译器。为了解决此问题,请在main.ts文件的顶部添加以下行:

import '@angular/compiler';

一旦你添加了以上内容,重新启动你的服务器和/或重新构建你的应用程序,这应该会解决它。

在我使用自定义weback配置的项目中,我有两个入口点文件main.tsbootstrap.ts

在两个文件的顶部添加import '@angular/compiler';为我解决了这个问题。非常感谢

最新更新