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

man*_*dav 8 migration jit aot angularjs angular

现在正在将应用程序从 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.
Run Code Online (Sandbox Code Playgroud)

以下是我用来配置此双启动过程的文件。

主要.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']);
});
Run Code Online (Sandbox Code Playgroud)

我不想import '@angular/compiler';在 main.ts 中执行此操作,尽管这似乎暂时有效,但它会在稍后迁移组件时导致类似的问题。

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

试过

  1. "postinstall": "ngcc --properties es2015 浏览器模块主 --first-only --create-ivy-entry-points"
  2. npm 更新
  3. webpack 配置中的 babel-loader。

Dev*_*ner 5

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

import '@angular/compiler';

添加上述内容后,重新启动服务器和/或再次构建应用程序,这应该可以修复它。