延迟加载在产品构建中损坏 Angular 8
Lazy loading broken on prod build Angular 8
我正在尝试让我的延迟加载路由在生产环境中工作。目前,在开发模式下一切正常,但是当我切换到 AOT 时出现错误:TypeError: Cannot read property 'routeConfig' of undefined
我用最少的代码在一个小型测试项目中重现了这个错误。我的延迟加载模块如下所示:
const routes: Routes = [
{
path: 'home',
component: HomeComponent
},
{
path: 'lazy',
loadChildren: () => import('./lazy-loaded-component/lazy-loaded.module').then(m => m.LazyLoadedModule)
},
{
path: '',
redirectTo: 'home',
pathMatch: 'full'
}
];
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
RouterModule.forRoot(routes, {useHash: true}),
FormsModule,
],
declarations: [
AppComponent,
HomeComponent
],
bootstrap: [AppComponent]
})
export class AppModule {
}
懒惰-loaded.module:
import {NgModule} from "@angular/core";
import {CommonModule} from "@angular/common";
import {RouterModule, Routes} from "@angular/router";
import {LazyLoadedComponent} from "./lazy-loaded.component";
const routes: Routes = [
{
path: '',
component: LazyLoadedComponent
}
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(routes)
],
exports: [RouterModule],
declarations: [LazyLoadedComponent]
})
export class LazyLoadedModule {
}
我的版本是这样的:
Angular CLI: 8.1.2
Node: 10.16.0
OS: win32 x64
Angular: 8.1.2
... animations, cli, common, compiler, core, elements, forms
... language-service, platform-browser, platform-browser-dynamic
... router, upgrade
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.801.2
@angular-devkit/build-angular 0.801.2
@angular-devkit/build-optimizer 0.8.9
@angular-devkit/build-webpack 0.801.2
@angular-devkit/core 8.1.2
@angular-devkit/schematics 8.1.2
@angular/compiler-cli 8.2.0-next.2
@ngtools/webpack 6.2.9
@schematics/angular 8.1.2
@schematics/update 0.801.2
rxjs 6.5.2
typescript 3.4.5
webpack 4.35.2
当我使用我们的自定义 webpack 构建时,我得到的原始错误是 Error: Runtime compiler is not loaded with production configuration
。我已经切换到使用 CLI,现在错误已经改变。我不确定这些信息是否有帮助,但我认为信息越多越好。
我试过清除 node_modules
,我试过 npm install acorn
,我试过删除所有旧的 webpack 插件和加载器来尝试消除冲突的 webpack 版本,我试过用于延迟加载的旧 Angular v7 字符串语法,以及许多其他内容。
Update 我刚刚尝试创建一个全新的 Angular 应用程序并重现了错误:Error: Runtime compiler is not loaded
因此,在下载 Angular sample lazy loading project 并使用在该项目的 package.json
中找到的依赖项后,我能够修复此错误。我已经将我所有的 @angular/
包降级为 ^8.0.0
并且设置 "@angular-devkit/build-angular": "0.800.0"
一旦我这样做了,我的延迟加载又开始工作了。我认为问题出在 @angular-devkit/build-angular
软件包的最新版本,但我不能确定。
我希望这对某人有所帮助,如果其他人遇到困难,我很乐意 post 任何其他支持代码!
我正在尝试让我的延迟加载路由在生产环境中工作。目前,在开发模式下一切正常,但是当我切换到 AOT 时出现错误:TypeError: Cannot read property 'routeConfig' of undefined
我用最少的代码在一个小型测试项目中重现了这个错误。我的延迟加载模块如下所示:
const routes: Routes = [
{
path: 'home',
component: HomeComponent
},
{
path: 'lazy',
loadChildren: () => import('./lazy-loaded-component/lazy-loaded.module').then(m => m.LazyLoadedModule)
},
{
path: '',
redirectTo: 'home',
pathMatch: 'full'
}
];
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
RouterModule.forRoot(routes, {useHash: true}),
FormsModule,
],
declarations: [
AppComponent,
HomeComponent
],
bootstrap: [AppComponent]
})
export class AppModule {
}
懒惰-loaded.module:
import {NgModule} from "@angular/core";
import {CommonModule} from "@angular/common";
import {RouterModule, Routes} from "@angular/router";
import {LazyLoadedComponent} from "./lazy-loaded.component";
const routes: Routes = [
{
path: '',
component: LazyLoadedComponent
}
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(routes)
],
exports: [RouterModule],
declarations: [LazyLoadedComponent]
})
export class LazyLoadedModule {
}
我的版本是这样的:
Angular CLI: 8.1.2
Node: 10.16.0
OS: win32 x64
Angular: 8.1.2
... animations, cli, common, compiler, core, elements, forms
... language-service, platform-browser, platform-browser-dynamic
... router, upgrade
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.801.2
@angular-devkit/build-angular 0.801.2
@angular-devkit/build-optimizer 0.8.9
@angular-devkit/build-webpack 0.801.2
@angular-devkit/core 8.1.2
@angular-devkit/schematics 8.1.2
@angular/compiler-cli 8.2.0-next.2
@ngtools/webpack 6.2.9
@schematics/angular 8.1.2
@schematics/update 0.801.2
rxjs 6.5.2
typescript 3.4.5
webpack 4.35.2
当我使用我们的自定义 webpack 构建时,我得到的原始错误是 Error: Runtime compiler is not loaded with production configuration
。我已经切换到使用 CLI,现在错误已经改变。我不确定这些信息是否有帮助,但我认为信息越多越好。
我试过清除 node_modules
,我试过 npm install acorn
,我试过删除所有旧的 webpack 插件和加载器来尝试消除冲突的 webpack 版本,我试过用于延迟加载的旧 Angular v7 字符串语法,以及许多其他内容。
Update 我刚刚尝试创建一个全新的 Angular 应用程序并重现了错误:Error: Runtime compiler is not loaded
因此,在下载 Angular sample lazy loading project 并使用在该项目的 package.json
中找到的依赖项后,我能够修复此错误。我已经将我所有的 @angular/
包降级为 ^8.0.0
并且设置 "@angular-devkit/build-angular": "0.800.0"
一旦我这样做了,我的延迟加载又开始工作了。我认为问题出在 @angular-devkit/build-angular
软件包的最新版本,但我不能确定。
我希望这对某人有所帮助,如果其他人遇到困难,我很乐意 post 任何其他支持代码!