未捕获(承诺):ChunkLoadError:加载块xxx-xxx-module失败。角度9

时间:2020-05-25 20:07:02

标签: angular routes lazy-loading angular9 code-splitting

角度9:延迟加载/路由器无法正常工作


控制台错误消息

ERROR Error: Uncaught (in promise): ChunkLoadError: Loading chunk frame-frame-module failed.


路径URL:/ author

http://local.xxxxx.com/node/190/author

代码

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { AppComponent } from './app/app.component';
import { HomeComponent } from './home/home.component';
import { BlankComponent } from './blank/blank.component';

const childRoutes = [
  {
    path: 'author/preview',
    data: {
      routeLabel: 'preview'
    },
    loadChildren: () => import('./frame/frame.module').then(m => m.FrameModule)
  },
  {
    path: 'author',
    canActivate: [DraftContentGuard, ContentEditorAccessGuard],
    data: {
      routeLabel: 'edit'
    },
    loadChildren: () => import('./frame/frame.module').then(m => m.FrameModule)
  },
];

const routes: Routes = [
  {
    path: ':locale/:type/:contentId',
    component: AppComponent,
    canActivate: [EnabledWebSegmentAccessGuard],
    children: childRoutes
  },
  {path: 'author', component: HomeComponent},
  {path: '', redirectTo: '/author', pathMatch: 'full'},
  {path: '**', component: BlankComponent}
];

@NgModule({
  imports: [RouterModule.forRoot(routes, {paramsInheritanceStrategy: 'always'})],
  exports: [RouterModule]
})
export class AppRoutingModule { }

frame.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { FrameRoutingModule } from './frame-routing.module';
import { FrameComponent } from './frame.component';

@NgModule({
    imports: [
        CommonModule,
        FrameRoutingModule
    ],
    declarations: [FrameComponent]
})

export class FrameModule { }

frame-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { FrameComponent } from './frame.component';


const routes: Routes = [
    {
        path: '',
        component: FrameComponent,
    }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})

export class FrameRoutingModule { }


项目结构

Project folders


我怎么看?

大多数文档,文章和教程使用的是不同的localhost服务器,因此我不确定服务器配置是否应该更改以适合延迟加载。

The file is not in my server

1 个答案:

答案 0 :(得分:2)

我也遇到同样的问题。然后我更改了以下内容

我不知道这是否正确,但是对我有用

{ path: '<$Path>',loadChildren:() => import('./<$module folder name>/<$YOUR Module name>.module').then(m => m.<$Modulename>)}  

我改为了

import {<$Modulename>} from '/<$Modulepath>';
{ path: '<$Path>', loadChildren:() => {return <$Modulename>;}  }