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

3pvhb19x  于 2022-11-21  发布在  Angular
关注(0)|答案(1)|浏览(359)

正在为应用程序从angularjs迁移到angular v13,我正在尝试双 Boot 应用程序。
并在浏览器控制台中出现以下错误:

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.

下面是我用来配置这个双 Boot 过程的文件。
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.“安装后”:“ngcc --属性es 2015浏览器模块主--仅第一个--创建Ivy入口点”

  1. npm更新
  2. Webpack配置中的Babel-Loader。
7kjnsjlb

7kjnsjlb1#

您很可能没有导入Angular 编译器。为了解决此问题,请在main.ts文件的顶部添加以下行:
import '@angular/compiler';
添加以上内容后,重新启动服务器和/或再次构建应用程序,这样应该可以修复它。

相关问题