【问题标题】:Error:JIT compilation failed for injectable class PlatformLocation {} while doing AngularJS to Angular 13 migration错误:在执行 AngularJS 到 Angular 13 迁移时,可注入类 PlatformLocation {} 的 JIT 编译失败
【发布时间】:2022-11-15 05:11:28
【问题描述】:

现在正在将应用程序从 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.

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

main.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 --properties es2015 浏览器模块 main --first-only --create-ivy-entry-points”
  2. npm 更新
  3. webpack 配置中的 babel-loader。

【问题讨论】:

  • 您找到解决方案了吗?

标签: angular angularjs migration jit aot


【解决方案1】:

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

import '@angular/compiler';

添加上述内容后,重新启动服务器和/或再次构建您的应用程序,这应该可以解决问题。

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 2022-12-19
    • 2021-05-25
    • 1970-01-01
    • 2023-01-24
    • 2022-07-04
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多