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

问题描述 投票:0回答:3

现在正在将应用程序从 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浏览器模块主--first-only --create-ivy-entry-points”
  2. npm 更新
  3. webpack 配置中的 babel-loader。
angular angularjs migration jit aot
3个回答
8
投票

您很可能缺少导入角度编译器。为了解决该问题,请在

main.ts
文件顶部添加以下行:

import '@angular/compiler';

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


0
投票

在我使用自定义 Webback 配置的项目中,我有 2 个入口点文件

main.ts
bootstrap.ts

在两个文件的顶部添加

import '@angular/compiler';
解决了我的问题。非常感谢你


0
投票

我的解决方案是删除 dist 和 .angular 文件夹。也许是缓存问题。

© www.soinside.com 2019 - 2024. All rights reserved.