我正在尝试从 JSON 文件加载路由。路由中还有一个延迟加载的模块。一切都按预期工作,直到代码在 aot 模式下的 ng build --prod 中运行。当我尝试转到延迟加载模块链接时,出现以下错误。代码如下:
应用程序路由模块.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule, Router } from '@angular/router';
import * as AppRoutingJson from '../assets/data/routing.json';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import { RouteoneComponent } from './routeone/routeone.component';
import { RoutetwoComponent } from './routetwo/routetwo.component';
const routes: Routes =[];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
entryComponents: [PageNotFoundComponent, RouteoneComponent, RoutetwoComponent],
})
export class AppRoutingModule {
constructor(private router: Router) {
this.prepareRoutes(AppRoutingJson);
}
prepareRoutes(routesJson: any) {
let routesArr = [] as Routes;
routesArr = [
{
path: 'routeone',
component: RouteoneComponent,
},
{
path: 'routetwo',
component: RoutetwoComponent,
},
{ path: 'contact', loadChildren: () => import('./contact/contact.module').then(m => m.ContactModule)},
{ path: 'dv', loadChildren: './modules/dv.module#DVModule' },
];
// routesArr=AppRoutingJson;
routesArr.forEach(route => {
routes.push(route);
});
routes.push(
{
path: 'page-not-found',
component: PageNotFoundComponent,
},
{
path: '**',
redirectTo: 'page-not-found',
}
);
console.log(routes);
this.router.resetConfig(routes);
}
}
错误如下: